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.

Comments?

Balbir

7 comments:

Gops said...

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

Do you see any deficiencies there?

Balbir said...

Gops,

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 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

Gops said...

Ballu,

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 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 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.

Ranking and Unranking permutations

I've been a big fan of Skiena's Algorithm Design Manual , I recently found my first edition of the book (although I own the third ed...