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
6
votes
1 answer

How to properly use a vector range constructor?

I want to load all the lines from a text file into a vector #include #include #include using namespace std; int main() { …
JLagana
  • 1,224
  • 4
  • 14
  • 33
6
votes
3 answers

Avoid the most vexing parse

How do I get the compiler to create temporaries, use the default constructor on them while not defining a function? struct B {}; template struct C {}; template struct A { A(const T& t,const T1& t1): m_t(t),m_t1(t1) { …
ritter
  • 7,447
  • 7
  • 51
  • 84
6
votes
1 answer

Is there any difference between `List x;` and `List x()`

The title comes from the famous site C++ FAQ by Marshall Cline. The author claims that there is a difference between the following two code examples. Suppose that List is the name of some class. Then function f() declares a local List object called…
maba
  • 47,113
  • 10
  • 108
  • 118
5
votes
2 answers

Understanding the C++ compiler

Possible Duplicate: Most vexing parse: why doesn't A a(()); work? I am having this simple C++ issue that is making me wanna restart my CS degree all over again trying to learn something this time. ;) Why this code doesn't compile: vector
5
votes
1 answer

Vector constructor with two parameters is parsed as a function declaration

Consider this example: #include #include #include #include int main() { std::string sen = "abc def ghi jkl"; std::istringstream iss(sen); std::vector // declaration in question …
jrok
  • 54,456
  • 9
  • 109
  • 141
5
votes
2 answers

What is the difference between these two versions of code?

This code causes compilation error (most vexing parse) #include class A { int a; public: A(int x) :a(x) {} }; class B { public: B(const A& obj) { std::cout << "B\n";} void foo() {std::cout <<…
bornfree
  • 2,308
  • 1
  • 23
  • 33
5
votes
2 answers

Confusion with move constructors: unable to call move constructor

I've been having difficulties understanding move constructors in C++. I have made a simple class with a default constructor, copy constructor, move constructor and destructor. Also, I have defined a function with two overloads, one accepts a…
5
votes
1 answer

How is this a most vexing parse?

I was going through this article and there is a statement in item 3 saying // C++98 rectangle w( origin(), extents() ); // oops, vexing parse how is the above a most vexing parse. If I did something like this struct origin { }; struct…
James Franco
  • 4,516
  • 10
  • 38
  • 80
5
votes
1 answer

Parameterless constructor

I am a fairly experienced .net developer but new to Arduino and C/C++ and I am trying to create my first library which is a simple driver for a 7 segment led display. I have many obtuse compiler errors but in the spirit of one thing at a time this…
Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
5
votes
2 answers

Function definition or variable definition?

Why does the compiler interpret this line as a function definition and not as a variable definition: Y y(X()); in the following code: #include struct X { X() { std::cout << "X"; } }; struct Y { Y(const X &x) { std::cout << "Y"; } …
Laura Maftei
  • 1,863
  • 1
  • 15
  • 25
5
votes
1 answer

Why is the address of an array element sometimes mistaken for a declaration?

I have some user defined iterators, and every now and then I get a strange error that is easy to work around, but I don't understand why I'm getting it: uint8_t bytes[pitch*height]; array_iterator::col_iterator a( &bytes[0]…
user1806566
  • 1,078
  • 6
  • 18
4
votes
1 answer

How the C++ most vexing parse is possible in this example?

I read that this code A a( A() ); was interpreted by the compiler as a function declaration while here I clearly see that A() is a function that returns an object. How can it be something else that the construction of a A object ? I've just read…
Autechre
  • 171
  • 7
4
votes
1 answer

use of std::less in std::map does not compile

I am unable to understand why does following function not compile #include #include int main(){ std::map> myMap(std::less()); myMap[2] = 2; std::cout << myMap[2] << std::endl; return 0; } The…
atuly
  • 193
  • 1
  • 10
4
votes
0 answers

Class initialization seems to be mistaken for function declaration

Take the following sample code: class Foo { public: Foo(int x) {} }; class Bar { public: Bar(const Foo &foo1, const Foo &foo2) {} }; void barfunc(const Bar &bar) {} void func() { int x = 1, y = 2; Bar bar(Foo(x), Foo(y)); …
Matt
  • 21,026
  • 18
  • 63
  • 115
4
votes
1 answer

C++ function definition and variable declaration mismatch?

Consider this very simple code: #include class Foo { public: Foo() {}; }; class Bar { public: Bar( const std::shared_ptr& foo ) {} }; int main() { Foo* foo = new Foo; Bar bar( std::shared_ptr( foo ) ); …
jpo38
  • 20,821
  • 10
  • 70
  • 151