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
21
votes
3 answers

Why is there no call to the constructor?

This code doesn't behave how I expect it to. #include using namespace std; class Class { Class() { cout<<"default constructor called"; } ~Class() { cout<<"destrutor called"; } }; int main() { …
mithilesh
  • 211
  • 1
  • 3
19
votes
2 answers

Is most vexing parse a formally defined concept

I was reading an SO post where one user made the following comment: Also note that ArrTest ar(); uses most vexing parse. But another user said the opposite: ArrTest ar(); is not the "most vexing parse". It's just a function declaration.…
Jason
  • 36,170
  • 5
  • 26
  • 60
19
votes
4 answers

A confusing detail about the Most Vexing Parse

My question is how the following line can be parsed as a function declaration: vector v(istream_iterator(cin), istream_iterator()); I understand most of the details of the Most Vexing Parse and why the second temporary iterator can…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
18
votes
2 answers

Absence of compilation error when using parametrized constructor

Today at work I came across a behavior in C++ which I don't understand. I have produced the following example code to illustrate my problem: #include #include class MyException { public: MyException(std::string s1)…
Gábor Ferencz
  • 243
  • 2
  • 9
15
votes
4 answers

C++ - What is this doing if the constructor is private?

In the code below, why does the compiler not complain for mClass2? class CMyClass{ private: CMyClass(){} }; void TestMethod(){ CMyClass mClass1; //Fails. CMyClass mClass2(); //Works. }
R4D4
  • 1,382
  • 2
  • 13
  • 34
15
votes
3 answers

C++ compile error constructing object with rvalue std::string

I'm faced with a compile error that I don't even know how to describe! It completely baffles me. The situation: Code tries to create an object on the stack with an rvalue std::string that is initialized with a char*. The code: #include…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
12
votes
2 answers

Difference between parsing of void() and int()

Having read about the most vexing parse, I experimented a bit and found this program. There are two very similar lines. One of them yields warnings in both g++7 and clang++-3.9, another does not. int main() { void(); // no warning int(); //…
Ivan Smirnov
  • 4,365
  • 19
  • 30
11
votes
4 answers

Why is the constructor not called when () is used to declare an object?

Possible Duplicate: Why is it an error to use an empty set of brackets to call a constructor with no arguments? $ cat cons.cpp #include class Matrix { private: int m_count; public: Matrix() { m_count = 1; …
Lazer
  • 90,700
  • 113
  • 281
  • 364
11
votes
6 answers

C++ spooky constructor

Possible Duplicate: Why is it an error to use an empty set of brackets to call a constructor with no arguments? Lets have this code class Foo { Foo(int) { } }; Then we have there results: int main() { Foo f1 = Foo(5); // 1: OK, explicit…
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
11
votes
1 answer

most vexing parse prevents in-class initializing a std::vector

C++11 allows in-class initialization: struct Foo{ std::vector v{3}; // vector of 3 empty strings }; If we wanted to initialize in-class a vector of ints, we would get something else: struct Foo{ std::vector v{3}; // vector…
xdavidliu
  • 2,411
  • 14
  • 33
10
votes
1 answer

Constructor not returning usable object

I have a problem with the constructor, which is not working as I'd expect. If I try to initialize my class like that, it will work and I get a usable object: vector v; MyClass<2> a(v); However, if I try to build a class like below (which…
dominos
  • 412
  • 1
  • 11
  • 21
10
votes
2 answers

How does this declaration invoke the Most Vexing Parse?

Consider the following program: #include struct A {}; int main(int argc, char** argv) { A a(std::fstream(argv[1])); } Clang in C++1y mode reckons that the MVP is invoked such that a is parsed as a function declaration: clang++…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
10
votes
2 answers

Uniform initialization syntax difference

What's the difference between doing A a{ A() }; and, A a( A{} ); to avoid the Most Vexing Parse? When should I use a particular one?
Me myself and I
  • 3,990
  • 1
  • 23
  • 47
9
votes
2 answers

Creating an instance of a class with ()

I have a question : what constructor is used when you create an instance of a class with ClassName instance() in C++ ? Example: #include using namespace std; class Test { private: Test() { cout << "AAA" << endl; …
Congelli501
  • 2,403
  • 2
  • 27
  • 28
9
votes
3 answers

Unusual compiler error casting to void?

I'm putting together a C++-based assignment for a class I'm teaching. I have a function I'm exporting to students that I'd like them to call at various points in their program so that, during grading, we can intercept those calls to make sure that…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
1
2
3
11 12