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
3
votes
2 answers

Understanding 'most vexing parse' - why allow ambiguous syntax?

While trying to understand the "Most vexing parse" problem in C/C++, this question immediately springs to mind - why have a syntax that causes this problem to begin with? For example, class Timer { public: Timer(); }; class…
Zach Saw
  • 4,308
  • 3
  • 33
  • 49
2
votes
3 answers

Return instance by value from a method stack compilation

Can you explain why this won't compile: (this is the error: ../Man.cpp:33:9: error: conversion from ‘Man (*)()’ to non-scalar type ‘Man’ requested) Code: Man goo(){ Man m(); return m; } but this does: Man goo(){ return Man(); }
ozma
  • 1,633
  • 1
  • 20
  • 28
2
votes
2 answers

Why can't I create a temporary of this templated class with an lvalue constructor argument?

For the life of me I can't seem to create a temporary of this class, why isn't it allowing me to? template struct Dog { Dog(T t) : tag(t) {} T tag; }; int main() { int tag = 6; Dog(6); // Works fine …
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
2
votes
1 answer

move sematics in thread wrapper class with RAII in C++

I am trying to get a RAII wrapper class around std::thread to work in C++. #include #include #include using std::cout; using std::endl; void threadFunction() { for (int i = 0; i < 20; ++i) { cout << "thread…
dhu
  • 718
  • 6
  • 19
2
votes
1 answer

Template class throwing error on non-parameterized constructors

I have a templated class with a parameterized constructor. Here's a minimal example. The following code works fine: template class my_template { public: my_template () {} my_template (T Value) : value(Value) {} T get_value…
Nightmare Games
  • 2,205
  • 6
  • 28
  • 46
2
votes
0 answers

Is the default constructor called differently when using parentheses versus nothing? (on a template class struct)

I've been writing some code for a project that includes OpenGL, and I've discovered weird behavior with my vec4 struct, of which I could not find an explanation. Here's the relevant snippet of my code: #include using namespace…
Drayux
  • 73
  • 6
2
votes
1 answer

Unclear most vexing parse

Let's assume the following class Foo. struct Foo { int i; }; if I want to make an instance of this class and initialize it, I should do: Foo foo1 = Foo(); to call the constructor. int main(void) { foo1 = Foo(); cout << foo1.i << " : " <<…
Guillaume D
  • 2,202
  • 2
  • 10
  • 37
2
votes
2 answers

Explain GCC error after using an object accidentally declared as a function

The following is a common typo with language newcomers, who think that they are defining an object but are actually declaring a function: struct T { void foo() {} }; int main() { T obj(); obj.foo(); } GCC 4.1.2's error is: In function…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2
votes
2 answers

C++ compile time checker using templates

I am having following code which is taken from modern C++ design. While i am using it i am getting compiation error i think invalid sizeof opearand. Can any one point out what is the problem. Thanks! template struct CompileTimeChecker { …
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
2
votes
1 answer

vexing parse in STL scott meyers

I am reding about Item6 which is about most vexing parse in Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library by Scott Meyers. ifstream dataFile("ints.dat"); list data(istream_iterator(dataFile), //…
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
2
votes
1 answer

Moving rvalue inside class member

I've been browsing for a while in search of an answer but I don't seem to find it. So I decided to specifically ask the question here. I've been trying to use something like this (and variants hereof): struct NonCopyable { NonCopyable() { }; …
racanu
  • 79
  • 1
  • 9
2
votes
0 answers

c++ priority_queue non-pod data type, push() compile error

I am trying to use the stl::priority_queue data struct, using a non-pod data type as elements of the PQ, and hence providing a custom comparison operator. There are some hacks in the comparison function object, which I will try to come up with a…
Ahmed A
  • 3,362
  • 7
  • 39
  • 57
2
votes
3 answers

Visual C++: No default constructor

I've looked at a couple other questions asking this, but mine seems to be a lot simpler of a case then the ones I've been through, so I'll ask my case for this. Learn.h: #ifndef LEARN_H #define LEARN_H class Learn { public: Learn(int x); …
Osama Kawish
  • 312
  • 1
  • 5
  • 15
2
votes
1 answer

Constructor interpreted as function call, and move constructors strangly skipped

I have a class with 4 constructors, and a function as below: using namespace std; class ABC { public: ABC() { cout << "ABC()\n"; } ABC(int) { cout << "ABC(int)\n"; } ABC(ABC&) { cout << "ABC(&)\n"; …
kien
  • 197
  • 9
2
votes
2 answers

explicitly using constructor call in main as a function call parameter

I am trying to understand how explicit constructor call in main works using the following code. #include using namespace std; class Dependency1 { bool init; public: Dependency1() : init(true) { std::cout << "Dependency1…
Anand
  • 73
  • 1
  • 5