Questions tagged [most-vexing-parse]

For questions about the "most vexing parse", a particular syntactic ambiguity in C++ programs that leads to a counterintuitive interpretation of certain declarations. (The name was coined by Scott Meyers in "Effective STL".) It is often accompanied by poor diagnostics, confusing many programmers who encounter it.

The most vexing parse refers to a particular C++ syntactic ambiguity where a declaration statement that can be interpreted either as an object declaration with an initializer that is a temporary object or as a function declaration is resolved, by rule, to a function declaration.

As an example, the line

MyObject obj(4);

declares an object of type MyObject named obj passing in 4 as a parameter. However, the line

MyObject obj(OtherType());

does not declare obj as an object constructed from a value initialized OtherType temporary, but instead declares a function named obj that takes a pointer to a function taking no arguments and returning an OtherType. (Function parameters declared as having function type are automatically adjusted to the corresponding pointer to function type in the same way that function parameters declared with array types are adjusted to the corresponding pointer type.)

To change the declaration into an object type the following alternatives can be used.

MyObject obj1( (OtherType()) ); // extra parentheses

MyObject obj1 = OtherType(); // Requires an implicit conversion between types
                             // and requires MyObject to be copyable (or movable
                             // in C++11)

MyObject obj1 {OtherType()}; // C++11 syntax
180 questions
227
votes
9 answers

Default constructor with empty brackets

Is there any good reason that an empty set of round brackets (parentheses) isn't valid for calling the default constructor in C++? MyObject object; // ok - default ctor MyObject object(blah); // ok MyObject object(); // error I seem to type…
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
168
votes
5 answers

My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it?

Among the many things Stack Overflow has taught me is what is known as the "most vexing parse", which is classically demonstrated with a line such as A a(B()); //declares a function While this, for most, intuitively appears to be the declaration of…
GRB
  • 3,982
  • 4
  • 27
  • 21
89
votes
2 answers

Why does C++ allow us to surround the variable name in parentheses when declaring a variable?

For example a declaration such as that: int (x) = 0; Or even that: int (((x))) = 0; I stumbled upon this because in my code I happened to have a fragment similar to the following one: struct B { }; struct C { C (B *) {} void f () {}; }; int…
Predelnik
  • 5,066
  • 2
  • 24
  • 36
81
votes
3 answers

I do not understand why this compiles

I'm certainly missing something, but I do not understand why this compiles (with both g++ & clang++): struct A { }; struct B { }; int main() { A a(B); } First of all, B is a type... not a value. How should I interpret this code?
Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70
80
votes
3 answers

Why does this call the default constructor?

struct X { X() { std::cout << "X()\n"; } X(int) { std::cout << "X(int)\n"; } }; const int answer = 42; int main() { X(answer); } I would have expected this to print either X(int), because X(answer); could be interpreted as a…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
71
votes
3 answers

Why won't this compile without a default constructor?

I can do this: #include int counter; int main() { struct Boo { Boo(int num) { ++counter; if (rand() % num < 7) Boo(8); } }; Boo(8); return 0; } This will compile…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
43
votes
6 answers

What is the purpose of the Most Vexing Parse?

On Wikipedia I found this: A a( A() ); [This] could be disambiguated either as a variable definition of class [A], taking an anonymous instance of class [A] or a function declaration for a function which returns an object of type [A] and…
template boy
  • 10,230
  • 8
  • 61
  • 97
38
votes
2 answers

Visual Studio C++ compiler weird behaviour

I'm just curious to know why this small piece of code compiles correctly (and without warnings) in Visual Studio. Maybe the result is the same with GCC and Clang, but unfortunately I can't test them now. struct T { int t; T() : t(0)…
c.bear
  • 1,197
  • 8
  • 21
32
votes
1 answer

Error calling user-defined operator+ on temporary object when there are extra brackets

If I have a user-defined operator+() as in: class A { public: A operator+(A) { return A(); } }; Then the following works as expected: A a = A() + A(); but g++-4.7 gives an error message on…
SirGuy
  • 10,660
  • 2
  • 36
  • 66
29
votes
1 answer

Most vexing parse

I got the code from here. class Timer { public: Timer(); }; class TimeKeeper { public: TimeKeeper(const Timer& t); int get_time() { return 1; } }; int main() { TimeKeeper time_keeper(Timer()); return…
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
28
votes
1 answer

Most vexing parse even more vexing

In the following code #include #include struct P2d { double x, y; P2d(double x, double y) : x(x), y(y) {} }; double bar() { std::map m; //P2d lp = P2d(double(m["x"]), double(m["y"])); // this works …
6502
  • 112,025
  • 15
  • 165
  • 265
24
votes
7 answers

Difference between creating object with () or without

i just run into the problem error: request for member ‘show’ in ‘myWindow’, which is of non-class type ‘MainGUIWindow()’ when trying to compile a simple qt-application: #include #include "gui/MainGUIWindow.h" int main( int argc,…
Dirk
  • 1,789
  • 2
  • 21
  • 31
24
votes
2 answers

g++ rejects, clang++ accepts: foo(x)("bar")("baz");

Somebody had asked the other day why something compiles with clang, but not with gcc. I intuitively understood what was happening and was able to help the person, but it got me wondering -- according to the standard, which compiler was correct? Here…
22
votes
2 answers

Most vexing parse with array access

While looking at some C++03 code, I found an instance of the most vexing parse that confused me: #include #include int main(int, char** argv) { std::stringstream ss(std::string(argv[0])); } live example on wandbox In the…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
21
votes
1 answer

Why does calling a functor with an undeclared variable work?

class foo { public: bool operator () (int & i) { return true; } }; int main() { foo(WhyDoesThisCompile); return 0; } When passing WhyDoesThisCompile (without spaces) to the functor, the program compiles. Why is this? I…
DanielCollier
  • 736
  • 2
  • 8
  • 21
1
2 3
11 12