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

Why can this code elide a copy?

Possible Duplicates: constructor invocation mechanism Why is it an error to use an empty set of brackets to call a constructor with no arguments? Why can this code elide all copies of A? #include class A { public: A() {} A(const…
Clinton
  • 22,361
  • 15
  • 67
  • 163
3
votes
0 answers

Why does clangd error with deduced_class_template_compound_type?

The following code fails to compile template struct A { A(T) {} }; int main() { int x = 3; A(x); // compile failure here } and the reasons seems to be explained here and here: A(x); is interpreted as A x;, a conflicting…
Enlico
  • 23,259
  • 6
  • 48
  • 102
3
votes
3 answers

Why this is not a vexing parse?

Basically this is a follow up of this question about most vexing parse. I can understand that this is due to the ambiguity between the function declaration and variable definition. But in Comeau online, I just tired the following. class…
liaK
  • 11,422
  • 11
  • 48
  • 73
3
votes
1 answer

error: declaration of ‘data’ as array of references

C++ application code was compiling fine with GCC 4.1. Now i upgraded the GCC version to 4.4X and i am getting an error. error: declaration of ‘data’ as array of references CODE: inline std::string base64_encode(const std::vector< unsigned char >&…
3
votes
1 answer

Constructor not throwing exception

In my code below, I wanted to test what would happen if I had an object, that contained another object whose constructor throws exception. But the code below does absolutely nothing. Nothing is printed on the console at all. class A { public: …
Kraken
  • 23,393
  • 37
  • 102
  • 162
3
votes
2 answers

Strange compiler error when trying to create a temporary object

After I posting this question I tried to reproduce the problem of accidental rvalue creation when creating a scoped RAII object. Now it appears that I can't reproduce it without compiler errors! In the following code sample, in Test::foo() the…
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
3
votes
1 answer

Can not instantiate class as LHS value with qualified enum as argument (C++ VS2015)

I'm a little bit supprised as i see this compilation error; Here is the example: class A { public: enum eA { eA1, eA2 }; class B { public: B(eA e, int i = 0); }; A(const B &b); }; A::A(const B &b) { } A::B::B(eA e, int i /*=…
3
votes
1 answer

Instance of Most Vexing Parse with std::string and char*

This is a follow-up to my previous question: C++ compile error constructing object with rvalue std::string from which I learned about the Most Vexing Parse. I understand now the gist of the problem, however there's one leftover item of syntax I…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
3
votes
1 answer

Why vexing parse in an if condition?

Consider the code: #include struct Foo { Foo(int){} operator bool() const { return true; } }; int main() { if(Foo foo{42}) { std::cout << "ok\n"; } } It compiles fine under gcc5. However, if…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
3
votes
1 answer

Why parenthesis after an object variable declaration cause error (C++)?

I created a class which basically serves as a public structure in this example, and let's say the class name is X. I want to declare a local object in main function. The short version of my question is: I know we can do X foo;, but I think X foo();…
TimeString
  • 1,778
  • 14
  • 25
3
votes
1 answer

Forcing non-temporary in RAII by compile Error C++

When one uses lock_guard in C++ like this: lock_guard(lock); The compiler complains: no matching constructor for initialization of 'std::lock_guard' Because the correct use is: lock_guard guard(lock); I would like to…
siemanko
  • 1,389
  • 1
  • 13
  • 26
3
votes
3 answers

Why do anonymous objects sometimes require a default constructor?

If I write the following program, it works as I expect: struct Foo { Foo (std::string x) { std::cout << x << std::endl; } }; int main () { Foo("hello, world"); } However, if I write a slightly different program, I get a compilation…
jxh
  • 69,070
  • 8
  • 110
  • 193
3
votes
4 answers

Default constructor c++

I am trying to understand how default constructor (provided by the compiler if you do not write one) versus your own default constructor works. So for example I wrote this simple class: class A { private: int x; public: A() {…
3
votes
2 answers

Why this statement does not call the constructors - C++

A template class and a normal class: template class Holder { public: Holder(const Type& value) : held_(value) { cout << "Holder(const Type& value)" << endl; } Type& Ref() { return held_; } private: Type…
Ggicci
  • 785
  • 2
  • 13
  • 29
3
votes
2 answers

structure expected on left side of . or .* but it is a structure

I'm getting the compile error structure required on left side of . or .* on chest.contents[0], but chest is a structure: class Item { public: int id; int dmg; }; class Chest { public: Item contents[10]; }; int main() { Chest…
Max
  • 51
  • 1
  • 6