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

Defining a lambda function in C++11 doesn't compile inside class

I was trying to create a lambda function inside a c++ class, but it is giving compilation error. Code looks like: class Test { public: struct V { int a; }; priority_queue, function> …
saha
  • 97
  • 8
0
votes
0 answers

Uniform Initialization Syntax Not Working on Visual Studio 2012

c++11 introduced Uniform Initialization Syntax as an explicit way to work around the Most Vexing Parse. I believe http://en.cppreference.com refers to it as Direct List Initialization. Anyway whatever you call it it doesn't seem to work in…
0
votes
2 answers

C++ Most vexing parse when a number literal is the argument?

I was making a class that looked like this: struct InputHandler { std::vector> keyBindings( 256 ); }; It came up with an error, and I know this is because the compiler interprets this as a function instead of a constructor…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
0
votes
1 answer

Why am I getting an error trying to read data from an istringstream?

I have the following C++ code: #include #include #include #include #include #include using namespace std; int main() { istringstream inSS; string title; string col1; string…
Chase
  • 93
  • 1
  • 1
  • 7
0
votes
0 answers

Why is the constructor not called when assigning a functor to a std::function?

I don't understand what happens with the following code using functors and std::function. My explanations are interlaced with the code (the concatenated code is compilable). #include #include class O { public: void…
Patrick B.
  • 11,773
  • 8
  • 58
  • 101
0
votes
1 answer

Expression must have a class type Error 153

This code for a 7segment display on an mbed, i am getting the error Error: Expression must have class type in "main.cpp", Line: 44, Col: 6 which is the line of seg_7.write(livewrite); I have tried different syntaxes and this seems to be the right…
Eddie Baker
  • 21
  • 1
  • 5
0
votes
1 answer

Differences between MSVC2013 and MingW lead to most vexing parse issue

I have an existing C++ and Qt program that builds fine under MSVC but fails to build with MinGW. Under MinGW I get lots of syntax errors such as: Redefinition of 'QString foobar' I have extracted the issue into a minimum working…
lanoxx
  • 12,249
  • 13
  • 87
  • 142
0
votes
2 answers

Does specifying the use of void in the declaration of a function that takes no arguments address The Most Vexing Parse?

Is the Most Vexing Parse rooted in the ambiguity about whether or not to use void as the parameter of a function declaration that takes no arguments? As an example, the following code compiles without error, and runs fine, on both the g++ (v7.2.1)…
kmiklas
  • 13,085
  • 22
  • 67
  • 103
0
votes
5 answers

Move ctor is not called

Am I doing something wrong (again)? #include using std::cout; struct Map { Map() { cout << "Map()\n"; } Map(const Map& pattern) { cout << "Map(const Map& pattern)\n"; } Map(Map&& tmp) { …
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
0
votes
1 answer

Simple trick to turn function declaration into variable definition

I know that the following statement is interpreted as a function declaration instead of a variable definition boost::system::system_error sys_err(boost::system::error_code()); Is there any simple trick to turn it into a one-liner variable…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
0
votes
1 answer

Converting std::string to QString in constructor

I cannot understand why std::string converted into QString while passing it to constructor. Here is small example: class StringHandler { private: QString str; public: StringHandler(QString s): str(s) {} }; int main(int argc, char…
Leo
  • 687
  • 1
  • 7
  • 14
0
votes
0 answers

Construction of class creates unexpected function pointer

I have the following code at the bottom of the post. The problem is: The resolved types are function pointers in a couple of cases although I expect to have instances of MyBar in all cases. The code is compiled with gcc 4.8.4 and nvcc (7.0 and 7.5)…
0
votes
1 answer

Calling a base class function from a derived class object. Base class data members set in derived class constructor

I have looked for the solution to the problem below all over the internet. Some of the solutions given are exactly what I am doing, yet I am still getting an error when I try to compile my code. Any help would be greatly appreciated. I have a base…
Carlos
  • 41
  • 1
  • 1
  • 2
0
votes
1 answer

Conversion from 'Type (__cdecl *)(std::istream)' to 'Type &'

I have problems to understand the cause of an error when calling a template function in C++. The function in question is part from rapidjson and the definition is like: template
Constantin
  • 8,721
  • 13
  • 75
  • 126
0
votes
1 answer

Force disambiguation of most vexing parse to be function declaration

All of the discussions I've seen on the "most vexing parse" talk about how to force the compiler to treat the case as a variable declaration with initializer (instead of the actual interpretation as a function declaration). See, for example, this…
jzions
  • 431
  • 3
  • 9
1 2 3
11
12