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

Meaning of I(I())

Sample code: typedef int I; struct X{ X(int); }; int main() { int(int()); X(X()); I(I()); } The line int(int()); is an expression using functional cast notation - it's a temporary int initialized with a value-initialized int. The line…
M.M
  • 138,810
  • 21
  • 208
  • 365
4
votes
1 answer

Why doesn't std::function work in this situation?

Imagine I have a type: struct my_type { double operator()(int a) { return 3.1415; } }; Then I'd like to wrap it in std::function. Consider two different approaches: my_type m_t; std::function
Aleksandr Tukallo
  • 1,299
  • 17
  • 22
4
votes
2 answers

is_function type trait for functors/function-objects

Consider the following piece of code: struct Bar { void operator()() {} }; int main() { std::cout << std::boolalpha << std::is_function::value << } The output is false. No surprises here since functor Bar does not qualify as a function…
101010
  • 41,839
  • 11
  • 94
  • 168
4
votes
2 answers

C++ copy constructor failure

I had a look at the various options suggested as questions that Stackoverflow thought might already have an answer, but nothing I saw came close. Sample code: #include class v2 { public: float x; float y; v2(float angle) :…
dgnuff
  • 3,195
  • 2
  • 18
  • 32
4
votes
3 answers

Calling class constructor with parameter - request of a member in 'x' which is of non-class type

I have a class A that accepts class B as a constructor parameter. Class B can be constructed from int value. My original code is quite complex, but I hope I've reduced it to the very base case: class B { public: explicit B(int a) : val(a)…
kamilm
  • 142
  • 1
  • 9
4
votes
1 answer

Inheriting from std::vector, compiler error? (most vexing parse)

For people who see this question: Take a look at the answer and consider using: cdecl Why does the code below give a compiler error: prog.cpp: In function ‘int main()’: prog.cpp:23:4: error: request for member ‘size’ in ‘a’, which is of…
Gabriel
  • 8,990
  • 6
  • 57
  • 101
4
votes
2 answers

C++11 Difference in Constructors (Braces)

I am quite new to C++ and have observed, that the following lines of code act differently MyClass c1; c1.do_work() //works MyClass c2(); c2.do_work() //compiler error c2228: left side is not a class, structure, or union. MyClass c3{}; c3.do_work()…
Ennosigaeon
  • 442
  • 7
  • 15
4
votes
1 answer

Most vexing parse: why doesn't `g( ( f() ) );` call `f`'s default constructor and pass the result to `g`'s ctor that takes a `f`?

This isn't a duplicate of Most vexing parse: why doesn't A a(()); work?, which is based on a parse in the form of A a(());, whose OP thought would be able to default-construct an A object using the extra set of parentheses. In contrast, my question…
CodeBricks
  • 1,771
  • 3
  • 17
  • 37
4
votes
2 answers

Is there a way to force the "most vexing parse" to be an error, even on a class by class basis?

Is it possible (with any modification of class A) to have the following work? i.e., make the most vexing parse an error? class A { }; int main() { A a(); // can this be forced to be an error?? A b; // this should work }
EHuhtala
  • 587
  • 3
  • 8
4
votes
2 answers

Most vexing parse and pointer indirection/dereferencing

Minimal code: struct A { A(int = 0) {} }; int i = 0, *p = &i; int* foo () { return p; } int main () { A(); // calls `A::A(int=0)` A(i); // calls `A::A(int=0)` A(*p); // <--- (1) same as local `A *p;` { A((*p)); // <--- (2) same…
iammilind
  • 68,093
  • 33
  • 169
  • 336
4
votes
3 answers

Calling constructor with a temporary object

I don't understand the following problem. class InnerBox { public: InnerBox() : mContents(123) { }; private: int mContents; }; class Box { public: Box(const InnerBox& innerBox) : mInnerBox(innerBox) { }; private: InnerBox…
Zero
  • 11,593
  • 9
  • 52
  • 70
4
votes
2 answers

Why do I need double parentheses  in constructor calls like: foo x( (bar()) );

Possible Duplicate: Why is it an error to use an empty set of brackets to call a constructor with no arguments? I have seen the C++ FQA entries about nested constructor calls and bracing and always wondered how C++ parsers resolve two and why it…
Alexander Oh
  • 24,223
  • 14
  • 73
  • 76
4
votes
4 answers

C++: bizarre occurrence of "Request for member X of Y which is of non-class type Z"

The following program, compiled with g++ 4.6, yields the error request for member ‘y’ in ‘a2’, which is of non-class type ‘A(B)’ at its last line: #include template class A { public: T y; A(T x):y(x){} }; class…
4
votes
2 answers

Why copy constructor not getting called in this case

Say, I have a class A Now when I am doing A a(A()); what exactly happens?
Kundan Kumar
  • 1,974
  • 7
  • 32
  • 54
3
votes
1 answer

How do I initialize a static random generator data member?

I have Random class, and I don`t know how correctly initialize its static data members. // random.h #pragma once #include class Random { private: static std::uniform_real_distribution sDistribution_; static std::mt19937…