Consider this code:
#include <iostream>
struct S
{
S(std::string s) : s_{s} { std::cout << "S( string ) c-tor\n"; }
S(S const&) { std::cout << "S( S const& ) c-tor\n"; }
S(S&& s) { std::cout << "S&& c-tor\n"; s_ = std::move(s.s_); }
S& operator=(S const&) { std::cout << "operator S( const& ) c-tor\n"; return *this;}
S& operator=(S&& s) { std::cout << "operator (S&&)\n"; s_ = std::move(s.s_); return *this; }
~S() { std::cout << "~S() d-tor\n"; }
std::string s_;
};
S foo() { return S{"blaaaaa"}; }
struct A
{
A(S s) : s_{s} {}
S s_;
};
struct B : public A
{
B(S s) : A(s) {}
};
int main()
{
B b(foo());
return 0;
}
When I compile it with g++ -std=c++1z -O3 test.cpp
, I get the following output:
S( string ) c-tor
S( S const& ) c-tor
S( S const& ) c-tor
~S() d-tor
~S() d-tor
~S() d-tor
I'm wondering why there is no copy elision? I expect something more like this:
S( string ) c-tor
~S() d-tor
There is the same output when I compile it with -fno-elide-constructors