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

Cannot access vector when constructing with istream_iterator range

I tried to compile this code snippet but I got compiler error :( ! Compile with Visual Studio 2010 #include #include #include #include #include using namespace std; int main() { string s(…
roxrook
  • 13,511
  • 40
  • 107
  • 156
2
votes
1 answer

Passing instances of objects in C++ class constructors (could not compile)

Here's my code class B { public: void bar() {std::cout<<"~";} }; class A { public: A() {b=B();}; A(B arg_b): b(arg_b) {}; void foo() {b.bar();}; private: B b; }; int main() { A a; a.foo(); // works fine A…
dontloo
  • 10,067
  • 4
  • 29
  • 50
2
votes
1 answer

Neither Default Constructor nor Copy Constructor is called when using anonymous object

Below is the snippet aimed to test the constructors. It was run in VS 2015. In my opinion, "B b(B())" has the same function as "B b = B()", however, my code seems to say that they behave different. I know there's copy elision by compiler…
2
votes
1 answer

How to value-initialize an object of a template type in C++03

Suppose I have a function template and want to declare a value-initialized object: template void foo() { // declare and default-initialize 'x' of type 'T' } Can I do it? T x; fails for primitive types because it leaves them…
EvanED
  • 947
  • 6
  • 22
2
votes
1 answer

Why isn't A's constructor invoked for the statement "A x(A())"?

This is a very basic question about C++. Why isn't a constructor invoked for the statement "A x(A())"? In the code that follows - which I have run with g++ 4.8.2 - the constructor (as well as the destructor) is called only once. This may be due to…
argmin
  • 77
  • 4
2
votes
0 answers

For what reason I get the "request for member ... in ... which is of non-class type ..." error in this case?

I've found a tricky (at least for me :D) issue in the following code: class Beta { public: Beta(double d) { value = d; } double getValue() const { return value; } …
2
votes
0 answers

C++ copy elision with ctor calls

I am trying to figure out how to better organize my code, so I've made a simple example representing the issue: Given ClassA and ClassB: class ClassA { public: ClassA() { cout << "ClassA ctor\n"; } ClassA(const ClassA &other) { cout <<…
Chebz
  • 1,375
  • 3
  • 14
  • 27
2
votes
2 answers

C++ Why is variable a function and not an object?

This title may not be completely accurate--it's based on my best guess on what is happening and I figured it was better than "Can someone explain what is happening with this code?" Anyway, I have this code: class Class1 { }; class Class2 { public: …
Hounddog
  • 385
  • 1
  • 10
2
votes
5 answers

C++ constructor not being called

I'm new to C++ and this is my first time with its classes and I was wondering, how do I call a constructor? I've read some documentation on classes in C++ and that's how I came up with what I have. The constructor calls private methods to setup the…
2
votes
3 answers

Meaning of class(*)() in gcc

I'm having trouble understanding this compiler error. I wrote class(*)() in the post title because the class I am instantiating is called "opaque", but that name is not informative or relevant. It is a templatized Circular Buffer and some tests. …
xst
  • 2,536
  • 5
  • 29
  • 41
1
vote
2 answers

Setting constructor default values in c++

Possible Duplicate: Why is it an error to use an empty set of brackets to call a constructor with no arguments? Constructing Objects and Calling Member functions Recently I've learnt a bit about constructors but today i've been having trouble…
Holly
  • 1,956
  • 6
  • 41
  • 57
1
vote
2 answers

why does C++ allow a declaration with no space between the type and a parenthesized variable name?

A previous C++ question asked why int (x) = 0; is allowed. However, I noticed that even int(x) = 0; is allowed, i.e. without a space before the (x). I find the latter quite strange, because it causes things like this: using Oit =…
xdavidliu
  • 2,411
  • 14
  • 33
1
vote
1 answer

(Why) is this instantiation of a template class with a temporary/anonymous object a vexing parse?

This code: // main.cpp template< typename T > class Owner { public: Owner( const T& t ) : t_( t ) {} void func() { } private: T t_; }; class Foo { }; int main( int argc, char* argv[] ) { Owner owner( Foo() ); owner.func(); …
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
1
vote
0 answers

why is only direct initialization prohibited for class member, not for local variable?

with respect to c++11 syntax, I understood that direct initialization of class member is not allowed(most vexing parse). I referred to this posting : Why class data members can't be initialized by direct initialization syntax? but I did not catch…
H.U.N.
  • 95
  • 9
1
vote
1 answer

Function Pointer declarations

The other day i was trying to create an object by calling the default constructor of another class, and it ended up making a function declaration, Here is an example: struct integer { integer(){} //Default constructor. }; struct rational { …