, , .
, , , , , f()
Test{}
, f()
. , t
, . BTW , .
:
struct Test
{
Test() { std::cout << "creation\n"; }
~Test() { std::cout << "destruction\n"; }
Test(Test&&) { std::cout << "move\n"; }
};
copy elision, :
before f call
creation // the temporary created inside f
move // return object move-constructed
destruction // the temporary destroyed
after f call
destruction // the returned object destroyed
LIVE
, $15.2/6 [class.temporary]:
, , :
(6.1) , , .
(6.2) ; return.
(6.3) , . [:
struct S { int mi; const std::pair<int,int>& mp; };
S a { 1, {2,3} };
S* p = new S{ 1, {2,3} };
- ] [. . - ]