Thursday, August 31, 2006

Special Treatment of Default Constructors

C++ inheritence from C is both a blessing and a curse. Here it is an example for the latter:
The part of C is that functions can be declared everywhere. The part of C++ is, that constructors are (almost)
treated like functions. This leads to ambiguity.

class Foo { };
int main()
{
Foo foo();
}

What is foo in this example? An object of type Foo or a forward declared function which
returns a Foo object and has no arguments?
The standard defines the latter. Unfortunately I have no clue what function declerations inside other functions are
good for. But nevertheless the possibility to do so was inherited from C. To avoid this ambiguity the C++ standard
defines that in order to instantiate an object of type Foo one needs to omit the parantheses.

int main()
{
Foo foo;
}

To make things worse this special rule applies only for the special case where this ambiguity occurs and not for all
cases where default constructors are used. For instance the following is a syntax error:

throw Foo;

If somebody knows some background to this ugly exception and contradiction please mail me. I would appreciate if I
could add some explanations to this facts.