The compiler understands what I want to do and adds the empty parentheses.
No it doesn't; the two expressions aren't quite equivalent. The difference is in how the objects are are initialised: the first uses default-initialisation, while the second uses value-initialisation. So they are equivalent for classes that define a default constructor; otherwise, the first will leave POD objects uninitialised, while the second will initialise them to zero.
why I am not allowed to write throw MyException;
?
MyException()
is an expression that creates a value-initialised temporary object; you can throw that just like you can throw the value of any other suitable expression.
MyException
isn't an expression; it's just a type name. You can only throw the value of an expression, so throw MyException;
is not valid. There's no way to create a default-initialised temporary.
To add some more confusion, the C++ FAQ suggests that the second usecase (new MyClass()) does not invoke a constructor, but calls function defined with operator() instead.
No it doesn't. It says that a declaration like List x();
declares a function with a return type List
, not (as one might think) a value-initialised object of type List
. It has nothing to do with new-expressions or operator()
.