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

Pass anonymous function object to std::function?

Here is my question: I define a functor: class A { public: int operator()(int a, int b) const{ return a + b; } }; typedef function Fun; then I use a anonymous functor to create a std::function object, and I find…
Liang Tan
  • 93
  • 4
8
votes
3 answers

How do I check if a value is contained in a vector? C++

I have a vector that I am trying to perform a contains function on. I am receiving some sort of casting error and I can't piece together a solution. I am also wanting to know whether or not what I am doing is the appropriate way to check if a vector…
LunchMarble
  • 5,079
  • 9
  • 64
  • 94
8
votes
2 answers

Most vexing parse

I saw a code here at Cpp Quiz [Question #38] #include struct Foo { Foo(int d) : x(d) {} int x; }; int main() { double x = 3.14; Foo f( int(x) ); std::cout << f.x << std::endl; return 0; } It is said there that this…
NeonGlow
  • 1,659
  • 4
  • 18
  • 36
8
votes
2 answers

Construction of temporary in function call is interpreted as declaration

Lately I ran into a problem which somehow (but only somehow) makes sense to me. It is based on interpreting the construction of a temporary as declaration of the single (!) constructor argument. Please have a look at the minimal example…
sedriel
  • 83
  • 4
8
votes
1 answer

C++11 ambiguity between declarator and abstract-declarator in parameter clause?

Consider the following C++11 code: struct C {}; void f(int(C)); Is the type of f the same as: typedef int T(C); void f(T); or is it like: void f(int C); That is, should the (C) be interpreted as a declarator of the parameter name C, or as an…
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
8
votes
1 answer

Is this a VS2008 bug? Functional style variable initialisation

In the code below, the line const char * const * eNames (names+cntNames); results in a C2061 error in Visual Studio 2008: syntax error : identifier 'identifier' - The compiler found an identifier where it wasn't expected. Make sure that…
Angus Comber
  • 9,316
  • 14
  • 59
  • 107
7
votes
2 answers

Why is my constructor with non const reference as argument allowed to be called with temporary objects?

I have a sample code below. #include template class XYZ { private: T & ref; public: XYZ(T & arg):ref(arg) { } }; class temp { int x; public: temp():x(34) { } }; template void fun(T…
chappar
  • 7,275
  • 12
  • 44
  • 57
7
votes
2 answers

constructor invocation mechanism

struct my { my(){ std::cout<<"Default";} my(const my& m){ std::cout<<"Copy";} ~my(){ std::cout<<"Destructor";} }; int main() { my m(); //1 my n(my()); //2 } Expected output : 1 ) Default 2 ) Copy Actual output : What's wrong with…
7
votes
2 answers

What's the differences between Test t; and Test t();? if Test is a class

Possible Duplicate: Why is there no call to the constructor? I am using Visual studio 2012, Suppose Test is a class class Test { }; When I create a new instance of Test, what's the difference of the following two ways? way 1 Test t; way 2 Test…
zdd
  • 8,258
  • 8
  • 46
  • 75
6
votes
4 answers

C++ declares a function instead of calling a complex constructor

First of, I know there are similar questions already on stackoverflow (this, this and this one) and that is why I understand the why of my problem. Unfortunately, that doesn't help me to solve it. While the above questions are all concerning the…
penelope
  • 8,251
  • 8
  • 45
  • 87
6
votes
1 answer

Why can the template not be instantiated in this piece of C++ code?

As titled, why is the template instantiation in the main function wrong? I use Clang++ with flag "-std=c++2a". Did I use anything in a wrong way here? template void f(int) { std::cout << "int"; }; template void g() { //…
6
votes
1 answer

std::chrono::time_point compiler error when converting from a variable

I have a variable of type long long that represents a time point in nanoseconds. I'm trying to wrap it using std::chrono::time_point yet the compiler (VS 2017) is giving me troubles. here is a code that…
Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37
6
votes
1 answer

Why does using parentheses with a default constructor result in creation of the variable?

After watching Louis Brandy talk at CppCon 2017 I was shocked to discover that this code actually compiles: #include int main() { std::string(foo); return 0; } And for some reason std::string(foo) it is identical to…
Amomum
  • 6,217
  • 8
  • 34
  • 62
6
votes
1 answer

Default argument allowing constructor to call private method

I have the class class A { public: class Key { Key() {} Key(Key const &) {} }; A(Key key, int a = 5) {} }; The constructor for Key is private, so no one should be able to construct an object A. However, with the…
R_Kapp
  • 2,818
  • 1
  • 18
  • 32
6
votes
1 answer

Most vexing parse with a qualified-id - or not?

Consider: struct Foo { enum { bar }; explicit Foo(int){} }; struct Baz { explicit Baz(Foo){} }; Baz b(Foo(Foo::bar)); // #1 Is line #1 the most vexing parse, even though Foo::bar is a qualified-id and can't possibly be a valid parameter…
T.C.
  • 133,968
  • 17
  • 288
  • 421
1 2
3
11 12