For the following code, I get in C++17:
move ctor
dtor
--------------------------------------------------------------------------------
dtor
Is this a possible output as well? i.e the move c'tor be elided and object be constructed in the caller side? Yes or No? And why?
--------------------------------------------------------------------------------
dtor
This is the code:
struct Foo {
Foo() {}
Foo(const Foo&) { std::cout << "copy ctor" << std::endl; }
Foo(Foo&&) { std::cout << "move ctor" << std::endl; }
~Foo() { std::cout << "dtor" << std::endl; }
};
Foo passit(Foo f) {
return f;
}
int main() {
Foo f = passit(Foo{});
std::cout << std::string(80, '-') << std::endl;
}
https://en.cppreference.com/w/cpp/language/copy_elision
Furthermore, it's hard to see how the compiler could decide on this line that the
Fooobject should be constructed at the address off, without knowing what happens withinpassit(), because this is effectively what would be required to optimise that copy out: