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
1
vote
3 answers

C++ constructor issues

I wonder if anybody can help with what seems to me like strange behaviour in c++ (gcc latest version). Below is some code that compiles successfully where I would expect a compile time error due to the lack of an appropriate constructor. Can anybody…
1
vote
1 answer

Bizarre unresolved external compiling DLL

I am trying to follow the instructions on this site: http://support.microsoft.com/kb/q168958 Here is the code: #include #include namespace Test { template class TestBuffer { private: TYPE* m_pData; …
Murrgon
  • 375
  • 4
  • 13
1
vote
2 answers

Parsing ambiguity in a call to a temporary function object

I suspect that in the code below, a call to a temporary function object with both a constructor argument and a function call operator argument is somehow ambiguous due to a most-vexing-parse issue. #include class Fun { public: …
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
0
votes
2 answers

Miscellaneous temporary object T()

Consider this code : int main() { int i(6); //this will result in i==6,but consider next initializations int j(int()); T * p2 = new T(); } I find that the value of j is 1, but this should be 0 because int() is a temporary with value…
T.J.
  • 1,466
  • 3
  • 19
  • 35
0
votes
0 answers

Is there a check in clang-tidy to reveal the most vexing parse cases?

I would like to speed up the compilation time of a legacy C++ project with millions of lines of code. I didn't find any compiler benchmarks focusing on refactoring the "most vexing parse" described by Scott Meyers…
Robert
  • 53
  • 1
  • 6
0
votes
0 answers

Why variable-sized object can be initialized with (), and not with {} in C++

Why can initialize array nums with parentheses, but cannot with braces? Whats the difference here? I know nums is variable sized object and cannot be initialized, but I am not sure why it can be initialized with zeros using parentheses. This…
Comizard
  • 45
  • 4
0
votes
1 answer

Template constructor: is this a case of the most vexing parse?

I have the following snippet (I'm compiling for C++20): struct Z { template Z(std::vector>) {} }; Z z(std::vector{std::tuple{}}); g++-10 refuses to compile this with the error: main.cpp:16:9: error:…
Svalorzen
  • 5,353
  • 3
  • 30
  • 54
0
votes
1 answer

Why do I need to declare a default constructor in order to compile when returning an unordered_map value?

This example fails to compile unless I uncomment the default constructor declaration: #include #include struct foo{ int data; /*foo(){ data = 0; std::cout << "DEFAULT\n"; }*/ foo(int d){ …
0
votes
0 answers

Most Vexing Parse: why do the extra parentheses work?

In order to avoid the Most Vexing Parse I added extra parentheses here: std::vector v(std::istream_iterator(inp), (std::istream_iterator())); Where inp is an istringstream created from a std::string. But why do the extra parentheses…
TRPh
  • 187
  • 1
  • 9
0
votes
1 answer

Purpose of function declaration within function definition

In C, one can of course declare and define functions. And this interacts with default constructor calls from C++ to yield the most vexing parse problem. Say I want to declare a function foo, define a function bar and create an instance t, I might…
Martin Ueding
  • 8,245
  • 6
  • 46
  • 92
0
votes
1 answer

why should the functor be placed inside the brackets while passing to thread costructor?

#include #include class DisplayThread { public: void operator()() { for(int i = 0; i < 10000; i++) std::cout<<"Display Thread Executing"<
StraightCirle
  • 182
  • 2
  • 12
0
votes
1 answer

A case od the most vexing parse

My code keeps giving me error Invalid operands to binary expression ('std::__1::ostream' (aka 'basic_ostream') and 'const bus') Is this a case of the most vexing parse? if so how to fix it. I am trying to print out objects stored in vector.…
cr34t1ve
  • 33
  • 3
0
votes
1 answer

c++ public members only accessible if constructor has a parameter

so I've created this class which contains some public members which are booleans, however, when I actually try to create an object with the default constructor (no parameters) I cannot access these members. However, if I declare the constructor with…
0
votes
1 answer

Compile error on constructor using pointer to constant c-string

Can somebody explain why I can compile and run this T t1( T2( "TEST") ); t1.print(); but not this const char * TEST_STRING = "TEST"; T t1( T2( TEST_STRING ) ); t1.print(); The 2nd blocks show error: request for member ‘print’…
0
votes
1 answer

complex class's default constructor doesn't work

According to http://www.cplusplus.com/reference/complex/complex/complex/, class complex has a default construct of the form complex (double re = 0.0, double im = 0.0);. However, I don't know if this is really correct. Could anyone explain…
ynn
  • 3,386
  • 2
  • 19
  • 42