tags:

views:

137

answers:

5

The following C++ code does nothing (using GCC 4.4.3) - it does not print the text:

struct MyStruct { MyStruct() { cout << "Hello" << endl; } };

void foo() {
  MyStruct ();
}

I think this is not so obvious... Let alone the danger of forgetting to give a variable name. Is there a compiler option/warning to forbid compilation of such code or is there any hidden secret behind allowing it?

Edit: I am sorry. The version above MyStruct(); acutally prints. The version which is not printing is:

void bar() {
    MyStruct a();
}

So now I am a bit confused.

+5  A: 
Prasoon Saurav
Would it optimize it out even though it's printing something?
JoshD
@JoshD: I am not a compiler expert but I don't think something like this is possible. I am not very sure though. Somebody please correct me if I am wrong.
Prasoon Saurav
+1  A: 

Well, temporary objects are perfectly legals. So there is no reasons to warn here.

Klaim
+6  A: 

This is not a declaration, because in MyStruct ();, the MyStruct would be part of the decl-specifier-seq and forms a type-name therein. And then () can only be a function-declarator. This requires that there is a declarator-id specified, which in your case is not. A special syntactical form is needed to allow such a syntax in declaring a constructor. But such syntactical exceptions are not made in a declaration-statement.

So this construct cannot be a declaration. It is parsed as an expression which specifies a functional cast creating a temporary of type MyStruct.

If the compiler does not print Hello it is either non-conforming or you are not calling foo in your program.


Your edit also does not specify a declaration without a name. It instead specifies a declaration that does have a name. It declares a function that is called a. The compiler has no way that you meant something else by this.

MyStruct a();

It could in an effort deduce this by having a recovering rule when it later discovers errors in your code such as the following

a.f();

If you have this in your code to try and call a member function and "a" is a function, the compiler could check to see if MyStruct contains a member f such that this expression is well-formed. But what if you just forgot to place parentheses? The following would be valid for the above declared function that returns a MyStruct, assuming a suitably declared member f.

a().f();

So in effect, the compiler can't really know what you mean.

Johannes Schaub - litb
I would expect `MyStruct a();` to be a definition calling the default constructor, like ` MyStruct a(1);` calling a constructor with one argument.
Danvil
@Danvil others may expect it to be a factory function returning a `MyStruct`. What about `std::string getName();` ?
Johannes Schaub - litb
@Danvil : `MyStruct a(1)` actually creates an object `a` but `MyStruct a()` doesn't. Please read his post carefully.
Prasoon Saurav
@Johannes: In global or namespace scope ok ..., but *inside* a function?
Danvil
@Prasoon: I understood his post well. Just wondering for the reason behind this design decision.
Danvil
@Danvil : Can't you declare a function inside another? Well in C++ you can.
Prasoon Saurav
I never declared a function inside a function in my C++ life. Perhaps I missed something - do you have a (useful) example for this? Nevertheless again my question: Does there exists a compiler option warning me that I am not defining an object but declaring a function?
Danvil
@Danvil You can declare functions that you define in namespace scope (and variables) inside a function. Like `void f() { extern int A; string g(); }` declaring `A` and `g`, both of which are members of the same namespace. The C language is flexible like that (C doesn't know the `type()` syntax, so it has no reason to worry about ambiguities), and C++ needs to be compatible to it here.
Johannes Schaub - litb
@Danvil moreover, since C does not have a scope resolution operator, it's the only way to make a function visible that is hidden by another identifier in a surrounding block in C: `void f(void) { ... } void g(void) { int f = 0; { void f(void); f(); } }`.
Johannes Schaub - litb
+3  A: 

You don't have "a declaration with no name" in your code. You have a completely valid expression MyStruct(). It is not a declaration, once again, it is an expression statement. If it doesn't print anything, it mist be the effect of optimization: in C++ it is generally allowed to eliminate temporaries, even if their constructors/destructors have side effects. (Although this context is not really one where such elimination is allowed. Are you sure you actually execute that expression statement?)

AndreyT
+3  A: 

MyStruct a(); declares a function a that takes no arguments and returns a MyStruct.

Steve M