most-vexing-parse

Sort function does not work with function object created on stack?

#include<iostream> #include<vector> #include<algorithm> class Integer { public: int m; Integer(int a):m(a){}; }; class CompareParts { public: bool operator()(const Integer & p1,const Integer & p2) { return p1.m<p2.m; } }obj1; int main() { std::vector<Integer> vecInteger; vecI...

const reference binding to an rvalue

Working on this question, I found an inconsistent behavior. Why reference binding behave different in a constructor from a common function? struct A { }; struct B : public A { B(){} private: B(const B&); }; void f( const B& b ) {} int main() { A a( B() ); // works A const & a2 = B(); // C++0x: works, C++03: fails f( B() ...

Most vexing parse: why doesn't A a(()); work?

Among the many things Stack Overflow has taught me is what is known as the "most vexing parse", which is classically demonstrated with a line such as A a(B()); //declares a function While this, for most, intuitively appears to be the declaration of an object a of type A, taking a temporary B object as a constructor parameter, it's act...

passing Temporary variables to reference arg in Cons works. but not for functions in general. Why?

Consider the following code. Here, A a(B()) compiles even though the constructor is A(B& b); But print(B()) does not work. But print is also declared as print(B& b); Why this inconsistency? #include <iostream> using namespace std; class B{ public: char b; }; class A { public: B b; A(B& b); ...

Is no parentheses on a C++ constructor with no arguments a language standard?

I was compiling a C++ program in Cygwin using g++ and I had a class whose constructor had no arguments. I had the lines: MyClass myObj(); myObj.function1(); And when trying to compile it, I got the message: error: request for member 'function1' in 'myObj', which is of non-class type 'MyClass ()()' After a little research, I found th...

Why is there no call to the constructor?

This code doesn't behave how I expect it to. #include<iostream> using namespace std; class Class { Class() { cout<<"default constructor called"; } ~Class() { cout<<"destrutor called"; } }; int main() { Class object(); } I expected the output 'default constructor called', but I did not...

Move ctor is not called

Am I doing something wrong (again)? #include <iostream> using std::cout; struct Map { Map() { cout << "Map()\n"; } Map(const Map& pattern) { cout << "Map(const Map& pattern)\n"; } Map(Map&& tmp) { cout << "Map(Map&& tmp)\n"; } }; Map createMap() { return Map(); } int m...