C++11. I'm trying to get a grasp on the basics of what constructor gets called when in what situation so the sake of preventing unnecessary copies and etc. In the below code sample I am not understanding why the Foo() constructor is getting called when I am passing in an argument. The only guess I can make is that the compiler might be optimizing away the constructor, but since this is changing behavior (by ignoring my cout statements) I would hope the compiler wouldn't do something like that.
class Foo {
public:
Foo() {
cout << "vanilla constructor" << endl;
}
Foo(const Foo& f) {
cout << "copy constructor" << endl;
}
Foo(Foo&& f) {
cout << "move constructor" << endl;
}
};
Foo getAFoo() {
return Foo();
}
int main() {
Foo f(getAFoo()); // prints "vanilla contructor" ???
return 0;
}
What am I missing - why isn't the move constructor being called?