Saturday, July 09, 2005

First Letter to Stroustrup

Hello, Bjarne,

I have been reading "The C++ Programming language, third edition (special edition)". Reading through section 11.3.2 made me realize that user defined types could use another feature like "type promotion" to get closer to types supported by the language. To support Mixed mode arithmetic the number of operators defined is 5 in section 11.3.2.

I was wondering if we have a promotion operator.

For example, lets say we have a reserved keyword promotion and we could do the following

Complex& operator promotion(double d)
this->im = 0;
this->re = d;
return *this;

The promotion operator could be invoked whenever we encounter a type in an operator that is not of the same class as the operator.

For example

2 + c // where c is complex

Could invoke a promotion operator with a new temporary created by the compiler

In effect, it would be equal to

t = new Complex();
c.operator+(t.operator promotion(2), c)

I think this would simpifly the permutations, combinations needed to develop a good usable user defined type.




Gops said...

Why not have an appropriate operator+? Or alternatively, a single argument constructor for the class?

Do you see any deficiencies there?

Balbir Singh said...


If you read through section 11.3.2 you will find that to support mixed mode arithmetic for a class as if they were built in types, we need several operators and overloaded implementation for each one of them.

Consider for example the complex class

2 + c // where c is complex

I would first need to construct a class for two and then add it to c
I would have to do the following

Complex t = 2;
t + c

or add many operators to the complex class.

Type promotion would help reduce the number of operators to be implemented.

Balbir Singh said...

Gops, it seems that you might be right after all -- constructors also do type conversion. Section 11.3.5 illustrates that point. But the conversion is not complete enough, it has some limitations

NGM said...

C# can convert basic to types to object types at runtime: Boxing

But it is limited to basic data types. This is very similat to what balbir calls 'promotion'

Yes but as Gopal has illustrated we can acheive the same through constructors

or much simpler is write a method in your class which returns the 'promoted' object when it is passed the basic data type

Gops said...


Can you enumerate the limitations you encountered? The one I see is that only one conversion is done per operation. So, if you only had a Complex(double d) constructor, doing 2+c (c = complex) wouldn't work.

Are there any others that I should be aware of?

Balbir Singh said...

I thought boxing was for conversion of built-in types like int to their object types like Int, but I could be wrong

Balbir Singh said...

The other limitations are operands to the left hand side of . and -> are not converted.

BTW, In 2+c 2 will be converted to complex

Anonymous said...

You are right about 2+c - I got an conversion error because of a different reason :)

As far as converting args on the left of a . or -> is concerned, no compiler should do it, as it could start interfering with regular arithmetic stuff too!

Just one point on what Narasimha said - if you write a method to return the 'upgraded' type given a basic type, you'll need to make it static and call it explicitly. So, you can no longer do 2 + c.

NGM said...


the last comment is correct, What i had in mind was:

Complex::Promote(2) + c;

Where the static method Complex::Promote returns the promoted object.

But is not easier to just write c+2 instead? Or is this case very common

Dynamic programming for the binomial coefficient

More fun things, this time with some visualisation of what happens when memoisation is used and what happens when we don't. I don'...