3

In my program I have

stringstream strumien(); //1
stringstream strumien;  // 2
strumien<<"napis "<<8<<endl;

and the first line generates the following error

invalid operands of types 'std::stringstream() {aka std::basic_stringstream()}' and 'const char [7]' to binary 'operator<<'

But the second one works properly. (of course always one of them is commented out)
So what is the difference between them? Because I always thought that they are equal definitions of an object.

Bart
  • 19,692
  • 7
  • 68
  • 77
Błażej
  • 151
  • 1
  • 7
  • 4
    The first one is probably MVP. It thinks youre declaring a function that takes no arguments and returns a stringstream. – Borgleader Aug 11 '13 at 20:03
  • And with that @Borgleader is referring to http://en.wikipedia.org/wiki/Most_vexing_parse FYI. – Bart Aug 11 '13 at 20:23

3 Answers3

8

Something which looks like a function declaration is a function declaration. That is

T name();

declares a function called name taking no arguments and returning a T. This is called the Most Vexing Parse. There are two potential fixes to avoiding this problem: leaving the parenthesis off or using C++2011 "uniform initialization" (which is a gross misnomer):

T name{};
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
1

The first line is in fact a declaration. Even if it seems to be the same as your second, it is not...

So

T name();

declares a function name return an object of type T.

The C++ standard states this ambiguity :

6.8 Ambiguity resolution [stmt.ambig]

There is an ambiguity in the grammar involving expression-statements and declarations: An expression-statement with a function-style explicit type conversion as its leftmost subexpression can be indistinguishable from a declaration where the first declarator starts with a (. In those cases the statement is a declaration.

This ambiguity is also called The Most Vexing Parse.


There is a new way to solve this since the C++11, it is called the uniform initialization syntax.

It works like :

T name{};
Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
0

The first line is ambiguous in the formal grammar: Either it could be interpreted as a function declaration or a declaration and instantiation of an object with a no-arg constructor. The language opts to treat it as a function declaration since it is possible to forego the parentheses to invoke the no-arg constructor.

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134