Temporary object argument lifetime in a function - c++

I've read several posts about temporary object's lifetime. And in a word I learn that:
the temporary is destroyed after the end of the full-expression
containing
it.
But this code is out of my expectation:
#include <memory>
#include <iostream>
void fun(std::shared_ptr<int> sp)
{
std::cout << "fun: sp.use_count() == " << sp.use_count() << '\n';
//I expect to get 2 not 1
}
int main()
{
fun(std::make_shared<int>(5));
}
So I think I have 2 smart pointer objects here, one is std::make_shared<int>(5), the temporary unnamed object and the other sp which is a local variable inside the function. So based on my understanding, the temporary one won't "die" before completing the function call. I expect output to be 2 not 1. What's wrong here?

Pre-C++17, sp is move-constructed from the temporary if the move is not elided to begin with. In either case, sp is the sole owner of the resource, so the use count is rightly reported as 1. This is overload 10)† in this reference.
While the temporary still exists, if not elided, it is in a moved-from state and no longer holds any resource, so it doesn't contribute to the resource's use count.
Since C++17, no temporary is created thanks to guaranteed copy/move elision, and sp is constructed in place.
† Exact wording from said reference:
10) Move-constructs a shared_ptr from r. After the construction, *this contains a copy of the previous state of r, r is empty and its stored pointer is null. [...]
In our case, r refers to the temporary and *this to sp.

c++ has a strange concept known as elision.
Elision is a process whereby the compiler is allowed to take the lifetime of two objects and merge them. Typically people say that the copy or move constructor "is elided", but what is really elided is the identity of two seemingly distinct objects.
As a rule of thumb, when an anonymous temporary object is used to directly construct another object, their lifetimes can be elided together. So:
A a = A{}; // A{} is elided with a
void f(A);
f(A{}); // temporary A{} is elided with argument of f
A g();
f(g()); // return value of g is elided with argument of f
There are also situations where named variables can be elided with return values, and more than two objects can be elided together:
A g() {
A a;
return a; // a is elided with return value of g
}
A f() {
A x = g(); // x is elided with return value of g
// which is elided with a within g
return x; // and is then elided with return value of f
}
A bob = f(); // and then elided with bob.
Only one instance of A exists in the above code; it just has many names.
In c++17 things go even further. Prior to that the objects in question had to logically be copyable/movable, and elision simply eliminated calls the the constructor and shared the objects identity.
After c++17 some things that used to be elision are (in some sense) "guaranteed elision", which is really a different thing. "Guaranteed elision" is basically the idea that prvalues (things that used to be temporaries in pre-c++17) are now abstract instructions on how to create an object.
In certain circumstances temporaries are instantiated from them, but in others they are just used to construct some other object in some other spot.
So in c++17 you should think of this function:
A f();
as a function that returns instructions on how to create a A. When you do this:
A a = f();
you are saying "use the instructions that f returns to construct an A named a".
Similarly, A{} is no longer a temporary but instructions no how to create an A. If you put it on a line by itself those instructions are used to create a temporary, but in most contexts no temporary logically or actually exists.
template<class T, class...Us>
std::shared_ptr<T> make_shared(Us&&...);
this is a function that returns instructions on how to create a shared_ptr<T>.
fun(std::make_shared<int>(5));
here you apply these instructions to the agument of fun, which is of type std::shared_ptr<int>.
In pre-[C++17] without hostile compiler flags, the result with elision is practically the same here. In that case, the temporaries identity is merged with the argument of fun.
In no practical case will there be a temporary shared_ptr with a reference count of 0; other answers which claim this are wrong. The one way where that can occur is if you pass in flags that your compiler from performing elision (the above hostile compiler flags).
If you do pass in such flags, the shared_ptr is moved-from into the argument of fun, and it exists with a reference count of 0. So use_count will remain 0.

In addition to the move construction of std::shared_ptr, there is another aspect to consider: in-place creation of function argument passed by value. This is an optimization that compilers usually do. Consider the exemplary type
struct A {
A() { std::cout << "ctor\n"; }
A(const A&) { std::cout << "copy ctor\n"; }
};
together with a function that takes an instance of A by value
void f(A) {}
When the function parameter is passed as an rvalue like this
f(A{});
the copy constructor won't be called unless you explicitly compile with -fno-elide-constructors. In C++17, you can even delete the copy constructor
A(const A&) = delete;
because the copy elision is guaranteed. With this in mind: the temporary object that you pass as a function argument is "destroyed after the end of the full-expression containing it" only if there is a temporary, and a code snippet might suggest the existence of one even though it's easily (and since C++17: guaranteed to be) optimized out.

Related

C++17 copy elision and object destruction

From cppreference,
When copy elision occurs, the implementation treats the source and
target of the omitted copy/move (since C++11) operation as simply two
different ways of referring to the same object, and the destruction of
that object occurs at the later of the times when the two objects
would have been destroyed without the optimization (except that, if
the parameter of the selected constructor is an rvalue reference to
object type, the destruction occurs when the target would have been
destroyed) (since C++17).
For a simple case like A a = returnA();, I can understand that the object is not destroyed in returnA() and instead the destruction occurs as in the case A a; which is the later time.
I can't think of a case which the opposite happens such that the source of the copy/move operation is destroyed first. Also I would like an example of the added statement since C++17 (exception when parameter of selected constructor is an rvalue reference to object type)
The symmetric case where the source outlives the target is when the prvalue is a parameter:
struct A {
static int *data;
A() {if(!refs++) data=new int(42);}
A(const A&) {++refs;} // not movable
~A() {if(!--refs) delete data;}
private:
static int refs;
};
int A::refs,*A::data;
int* f(A) {return A::data;}
A returnA();
int returnInt() {return *f(returnA());} // ok
Because the result of returnA() is a temporary, its lifetime extends to the end of the return statement’s full-expression. The implementation may identify it with f’s parameter, but may not destroy it when f returns, so the dereference in returnInt is valid. (Note that parameters may survive that long anyway.)
The adjustment in C++17 (along with such elision being guaranteed) is that if you (would) move the prvalue, it may be destroyed when the parameter is (since you shouldn’t be relying on its contents anyway). If that’s when f returns, the (ill-advised) code above becomes invalid if A is made movable.

Weird behavior when passing argument by value

Stumbled upon few articles claiming that passing by value could improve performance if function is gonna make a copy anyway.
I never really thought about how pass-by-value might be implemented under the hood. Exactly what happens on stack when you do smth like this: F v = f(g(h()))?
After pondering a bit I came to conclusion that I'd implement it in such way that value returned by g() is created in locations where f() expects it to be. So, basically, no copy/move constructor calls -- f() will simply take ownership of object returned by g() and destroy it when execution leaves f()'s scope. Same for g() -- it'll take ownership of object returned by h() and destroy it on return.
Alas, compilers seem to disagree. Here is the test code:
#include <cstdio>
using std::printf;
struct H
{
H() { printf("H ctor\n"); }
~H() { printf("H dtor\n"); }
H(H const&) {}
// H(H&&) {}
// H(H const&) = default;
// H(H&&) = default;
};
H h() { return H(); }
struct G
{
G() { printf("G ctor\n"); }
~G() { printf("G dtor\n"); }
G(G const&) {}
// G(G&&) {}
// G(G const&) = default;
// G(G&&) = default;
};
G g(H) { return G(); }
struct F
{
F() { printf("F ctor\n"); }
~F() { printf("F dtor\n"); }
};
F f(G) { return F(); }
int main()
{
F v = f(g(h()));
return 0;
}
On MSVC 2015 it's output is exactly what I expected:
H ctor
G ctor
H dtor
F ctor
G dtor
F dtor
But if you comment out copy constructors it looks like this:
H ctor
G ctor
H dtor
F ctor
G dtor
G dtor
H dtor
F dtor
I suspect that removing user-provided copy constructor causes compiler to generate move-constructor, which in turn causes unnecessary 'move' which doesn't go away no matter how big objects in question are (try adding 1MB array as member variable). I.e. compiler prefers 'move' so much that it chooses it over not doing anything at all.
It seems like a bug in MSVC, but I would really like someone to explain (and/or justify) what is going on here. This is question #1.
Now, if you try GCC 5.4.0 output simply doesn't make any sense:
H ctor
G ctor
F ctor
G dtor
H dtor
F dtor
H has to be destroyed before F is created! H is local to g()'s scope! Note that playing with constructors has zero effect on GCC here.
Same as with MSVC -- looks like a bug to me, but can someone explain/justify what is going on here? That is question #2.
It is really silly that after many years of working with C++ professionally I run into issues like this... After almost 4 decades compilers still can't agree on how to pass values around?
For passing a parameter by value, the parameter is a local variable to the function, and it's initialized from the corresponding argument to the function call.
When returning by value, there is a value called the return value. This is initialized by the "argument" to the return expression. Its lifetime is until the end of the full-expression containing the function call.
Also there is an optimization called copy elision which can apply in a few cases. Two of those cases apply to returning by value:
If the return value is initialized by another object of the same type, then the same memory location can be used for both objects, and the copy/move step skipped (there are some conditions on exactly when this is allowed or disallowed)
If the calling code uses the return value to initialize an object of the same type, then the same memory location can be used for both the return value and the destination object, and the copy/move step is skipped. (Here the "object of the same type" includes function parameters).
It is possible for both of these to apply simultaneously. Also, as of C++14, copy elision is optional for the compiler.
In your call f(g(h())), here is the list of objects (without copy elision):
H default-constructed by return H();
H, the return value of h(), is copy-constructed from (step 1).
~H (step 1)
H, the parameter of g, is copy-constructed from (step 2).
G default-constructed by return G();
G, the return value of g(), is copy-constructed from (step 5).
~G (step 5)
~H (step 4) (see below)
G, the parameter of f, is copy-constructed from (step 6).
F default-constructed by return F();
F, the return value of f(), is move-constructed from (step 10).
~F (step 10)
~G (step 9) (see below)
F v is move-constructed from (step 11).
~F, ~G, ~H (steps 2, 6, 11) are destroyed - I think there is no required ordering of the three
~F(step 14)
For copy elision, steps 1+2+3 can be combined into "Return value of h() is default-constructed". Similarly for 5+6+7 and 10+11+12. However it is also possible to combine either 2+4 on their own into "Parameter of g is copy-constructed from 1", and also possible for both of these elisions to apply simultaneously , giving "Parameter of g is default-constructed".
Because copy elision is optional you may see different results from different compilers. It doesn't mean there is a compiler bug. You'll be glad to hear that in C++17 some copy elision scenarios are being made mandatory.
Your output in the second MSVC case would be more instructive if you included output text for the move-constructor. I would guess that in the first MSVC case it performed both simultaneous elisions that I mentioned above, whereas the second case omits the "2+4" and "6+9" elisions.
below: gcc and clang delay destruction of function parameters until the end of the full-expression that enclosed the function call. This is why your gcc output differs from MSVC.
As of the C++17 drafting process, it is implementation-defined whether these destructions occur where I had them in my list, or at the end of the full-expression. It could be argued that it was insufficiently specified in the earlier published standards. See here for further discussion.
This behavior is because of an optimization technique called copy elision. In a nutshell all of outputs you mentioned are valid! Yep! Because this technique is (the only one) allowed to modify the behavior of the program. More information can be found at What are copy elision and return value optimization?
Both M.M's and Ahmad's answers were sending me in right direction, but they both weren't fully correct. So I opted to write down a proper answer below...
function call and return in C++ has following semantic:
value passed as function argument gets copied into function scope and function gets invoked
return value gets copied into caller's scope, gets destroyed (when we reach end of return full expression) and execution leaves function scope
When it comes to implementing this on IA-32-like architecture it becomes painfully obvious that these copies are not required -- it is trivial to allocate uninitialized space on stack (for return value) and define function calling conventions in such way that it knows where to construct return value.
Same for argument passing -- if we pass rvalue as function argument, compiler can direct creation of that rvalue in such way that it will be created right were (subsequently called) function expects it to be.
I imagine this is main reason why copy elision was introduced to standard (and is made mandatory in C++17).
I am familiar with copy elision in general and read this page before. Unfortunately I missed two things:
the fact that this also applies to initialization of function arguments with rvalue (C++11 12.8.p32):
when a temporary class object that has not been bound to a reference
(12.2) would be copied/moved to a class object with the same
cv-unqualified type, the copy/move operation can be omitted by
constructing the temporary object directly into the target of the
omitted copy/move
when copy elision kicks in it affects object lifetime in a very peculiar way:
When certain criteria are met, an implementation is allowed to omit
the copy/move construction of a class object, even if the copy/move
constructor and/or destructor for the object have side effects. In
such cases, the implementation treats the source and target of the
omitted copy/move operation as simply two different ways of referring
to the same object, and the destruction of that object occurs at the
later of the times when the two objects would have been destroyed
without the optimization. This elision of copy/move operations, called
copy elision, is permitted in the following circumstances (which may
be combined to eliminate multiple copies)
This explains GCC output -- we pass some rvalue into a function, copy elision kicks in and we end up with one object being referred via two different ways and lifetime = longest of all of them (which is a lifetime of temporary in our F v = ...; expression). So, basically, GCC output is completely standard compliant.
Now, this also means that MSVC is not standard compliant! It successfully applied both copy elisions, but resulting object lifetime is too short.
Second MSVC output conforms the standard -- it applied RVO, but decided to not apply copy elision for function parameter. I still think it is a bug in MSVC, even though code is ok from standard point of view.
Thank you both M.M and Ahmad for pushing me in right direction.
Now little rant about lifetime rule enforced by standard -- I think it was meant to be used only with RVO.
Alas it doesn't make a lot of sense when applied to eliding copy of function argument. In fact, combined with C++17 mandatory copy elision rule it permits crazy code like this:
T bar();
T* foo(T a) { return &a; }
auto v = foo(bar())->my_method();
this rule forces T to be destroyed only at the end of full expression. This code will become correct in C++17. It is ugly and should not be allowed in my opinion. Plus, you'll end up destroying these objects on caller side (instead of inside of a function) -- needlessly increasing code size and complicating process of figuring out if given function is a nothrow or not.
In other words, I personally prefer MSVC output #1 (as most 'natural'). Both MSVC output #2 and GCC output should be banned. I wonder if this idea can be sold to C++ standardization committee...
edit: apparently in C++17 lifetime of temporary will become 'unspecified' thus allowing MSVC's behavior. Yet another unnecessary dark corner in the language. They should have simply mandated MSVC's behavior.

Can't return unique_ptr element from an array by value [duplicate]

unique_ptr<T> does not allow copy construction, instead it supports move semantics. Yet, I can return a unique_ptr<T> from a function and assign the returned value to a variable.
#include <iostream>
#include <memory>
using namespace std;
unique_ptr<int> foo()
{
unique_ptr<int> p( new int(10) );
return p; // 1
//return move( p ); // 2
}
int main()
{
unique_ptr<int> p = foo();
cout << *p << endl;
return 0;
}
The code above compiles and works as intended. So how is it that line 1 doesn't invoke the copy constructor and result in compiler errors? If I had to use line 2 instead it'd make sense (using line 2 works as well, but we're not required to do so).
I know C++0x allows this exception to unique_ptr since the return value is a temporary object that will be destroyed as soon as the function exits, thus guaranteeing the uniqueness of the returned pointer. I'm curious about how this is implemented, is it special cased in the compiler or is there some other clause in the language specification that this exploits?
is there some other clause in the language specification that this exploits?
Yes, see 12.8 §34 and §35:
When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object [...]
This elision of copy/move operations, called copy elision, is permitted [...]
in a return statement in a function with a class return type, when the expression is the name of
a non-volatile automatic object with the same cv-unqualified type as the function return type [...]
When the criteria for elision of a copy operation are met and the object to be copied is designated by an lvalue,
overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue.
Just wanted to add one more point that returning by value should be the default choice here because a named value in the return statement in the worst case, i.e. without elisions in C++11, C++14 and C++17 is treated as an rvalue. So for example the following function compiles with the -fno-elide-constructors flag
std::unique_ptr<int> get_unique() {
auto ptr = std::unique_ptr<int>{new int{2}}; // <- 1
return ptr; // <- 2, moved into the to be returned unique_ptr
}
...
auto int_uptr = get_unique(); // <- 3
With the flag set on compilation there are two moves (1 and 2) happening in this function and then one move later on (3).
This is in no way specific to std::unique_ptr, but applies to any class that is movable. It's guaranteed by the language rules since you are returning by value. The compiler tries to elide copies, invokes a move constructor if it can't remove copies, calls a copy constructor if it can't move, and fails to compile if it can't copy.
If you had a function that accepts std::unique_ptr as an argument you wouldn't be able to pass p to it. You would have to explicitly invoke move constructor, but in this case you shouldn't use variable p after the call to bar().
void bar(std::unique_ptr<int> p)
{
// ...
}
int main()
{
unique_ptr<int> p = foo();
bar(p); // error, can't implicitly invoke move constructor on lvalue
bar(std::move(p)); // OK but don't use p afterwards
return 0;
}
unique_ptr doesn't have the traditional copy constructor. Instead it has a "move constructor" that uses rvalue references:
unique_ptr::unique_ptr(unique_ptr && src);
An rvalue reference (the double ampersand) will only bind to an rvalue. That's why you get an error when you try to pass an lvalue unique_ptr to a function. On the other hand, a value that is returned from a function is treated as an rvalue, so the move constructor is called automatically.
By the way, this will work correctly:
bar(unique_ptr<int>(new int(44));
The temporary unique_ptr here is an rvalue.
I think it's perfectly explained in item 25 of Scott Meyers' Effective Modern C++. Here's an excerpt:
The part of the Standard blessing the RVO goes on to say that if the conditions for the RVO are met, but compilers choose not to perform copy elision, the object being returned must be treated as an rvalue. In effect, the Standard requires that when the RVO is permitted, either copy elision takes place or std::move is implicitly applied to local objects being returned.
Here, RVO refers to return value optimization, and if the conditions for the RVO are met means returning the local object declared inside the function that you would expect to do the RVO, which is also nicely explained in item 25 of his book by referring to the standard (here the local object includes the temporary objects created by the return statement). The biggest take away from the excerpt is either copy elision takes place or std::move is implicitly applied to local objects being returned. Scott mentions in item 25 that std::move is implicitly applied when the compiler choose not to elide the copy and the programmer should not explicitly do so.
In your case, the code is clearly a candidate for RVO as it returns the local object p and the type of p is the same as the return type, which results in copy elision. And if the compiler chooses not to elide the copy, for whatever reason, std::move would've kicked in to line 1.
One thing that i didn't see in other answers is To clarify another answers that there is a difference between returning std::unique_ptr that has been created within a function, and one that has been given to that function.
The example could be like this:
class Test
{int i;};
std::unique_ptr<Test> foo1()
{
std::unique_ptr<Test> res(new Test);
return res;
}
std::unique_ptr<Test> foo2(std::unique_ptr<Test>&& t)
{
// return t; // this will produce an error!
return std::move(t);
}
//...
auto test1=foo1();
auto test2=foo2(std::unique_ptr<Test>(new Test));
I would like to mention one case where you must use std::move() otherwise it will give an error.
Case: If the return type of the function differs from the type of the local variable.
class Base { ... };
class Derived : public Base { ... };
...
std::unique_ptr<Base> Foo() {
std::unique_ptr<Derived> derived(new Derived());
return std::move(derived); //std::move() must
}
Reference: https://www.chromium.org/developers/smart-pointer-guidelines
I know it's an old question, but I think an important and clear reference is missing here.
From https://en.cppreference.com/w/cpp/language/copy_elision :
(Since C++11) In a return statement or a throw-expression, if the compiler cannot perform copy elision but the conditions for copy elision are met or would be met, except that the source is a function parameter, the compiler will attempt to use the move constructor even if the object is designated by an lvalue; see return statement for details.

Understanding function call in context of temporary objects

Look at this simple code:
class A
{};
A f(A a)
{
return a;
}
int main(void)
{
A a;
A b = f(a);
return 0;
}
It creates a local variable a, calls a function f() and assigns its return value to another variable b. But I'd like to know what happens during the function call.
Could someone describe to be me, step by step, what objects (temporary or otherwise) are created during the process, what constructors, destructors and assign/move operators are called and when?
When in doubt bring out the Noisy class:
struct Noisy {
Noisy() { std::cout << "Default construct" << std::endl; }
Noisy(const Noisy&) { std::cout << "Copy construct" << std::endl; }
Noisy(Noisy&&) { std::cout << "Move construct" << std::endl; }
Noisy& operator=(const Noisy&) { std::cout << "Copy assignment" << std::endl; return *this; }
Noisy& operator=(Noisy&&) { std::cout << "Move assignment" << std::endl; return *this; }
~Noisy() { std::cout << "Destructor" << std::endl; }
};
Noisy f(Noisy a) {
return a;
}
int main(void) {
Noisy a;
Noisy b = f(a);
}
Compiled with gcc-4.9.1 using options g++ -fno-elide-constructors -std=c++11 t.cc gives output:
Default construct // 1. 'a' is default constructed.
Copy construct // 2. Local argument 'a' in function 'f' is copied.
Move construct // 3. Return value is move constructed (*see note below).
Move construct // 4. 'b' is move constructed from return value.
Destructor // 5. Local argument 'a' is destroyed.
Destructor // 6. Return value is destroyed.
Destructor // 7. 'b' is destroyed.
Destructor // 8. 'a' is destroyed.
Note: Even though local argument a is an lvalue, the compiler knows it's about to go out of scope and considers it as an rvalue.
Compiling without option -fno-elide-constructors will enable compiler copy elision optimizations and yields output:
Default construct // 1. 'a' is default constructed.
Copy construct // 2. Local argument 'a' in function 'f' is copied.
Move construct // 3. 'b' is move constructed from argument 'a' (elision).
Destructor // 4. Local argument 'a' is destroyed.
Destructor // 5. 'b' is destroyed.
Destructor // 6. 'a' is destroyed.
Compiling with -std=c++03 i.e. C++03 will result in all moves being replaced with copies.
For more info about copy elision see here: What are copy elision and return value optimization?
A f(A a)
{
return a;
}
A a;
A b = f(a);
The parameter (a) is copy-initialized with the corresponding argument (a). That simply involves the copy-constructor.
The return-value temporary is copy-initialized with a.
b is copy-initialized with the return value of the function call. The implicitly-defined move-constructor is called (as the initializer is a (p)rvalue).
Note that copy elision doesn't apply here as, in return statements, it only works for variables that aren't function (or catch-clause) parameters.
int main(void)
{
A a; // creates `a` using default constructor of `A`
A b = f(a); // initializes `b` using implicitly-defined
// move-constructor from temporary copy of `a` (see [1])
/* where
A f(A a) // gets copy of `A` object as argument
{
return a; // return-value is copy-initialized
}
*/
return 0;
}
[1] The implicit generation of move constructors
Assuming the optimizer does not simplify the process:
The function f you wrote takes its argument a by value. So calling the function f invokes setting up the parameter to f, which means copying the local variable a of your main function into the stack space used for passing the parameter to f (a temporary object). This object is created using the copy constructor.
As f returns by value, all typical C++ implementations work by passing a pointer to storage space for an object of type A (no created object yet) as a hidden parameter to f. In the most simple case, this is a temporary object created on the stack of main for the time of the expression involving the function call.
Now that the parameters for f are set up, the function f is entered. As its sole statement is a return statement, the only thing f does is copying the parameter a into the storage provided by the caller. This is done using the move constructor, but you hit a corner case in the language specification here, so compiler behaviour might vary, and you might get a copy instead. More in this in the last paragraph.
After having constructed the return value, f exits. main regains control and creates the local variable b, taking the temporary object returned by f as source. As this temporary object is an rvalue, the initialization of b is done using move construction (on C++11).
As the statement is completed now, the temporary objects (the parameter and the return value) are destroyed. I don't think the order of destruction is specified, but if it is, it will be last-constructed first, so the return value would be destroyed before the parameter.
Typically, a "move elision" optimization is applied to your code, though. The hidden parameter of f is not given the address of a temporary object which is afterwards moved constructed into b, but f constructs directly int b.
The (non-named) return value optimization is not of a concern for the code given, as the return statement does not consist of a constructor call.
The named return-value optimization is also not applicable. This optimization would place the object you return (i.e. the parameter a) at the place the caller provided for the return value, so a copy/move operation can be avoided. In the function you wrote, the returned object is a parameter, so the compiler gets no chance to "put it where the return value is going to be" while compiling f, as the machine calling convention dictates where that object is to be found.
The before-mentioned corner case in the language specification (whether the return value of f is move- or copy-constructed) is rooted in an implicit "std::move", allowing moving, on the return statement. This is specified in clause [class.copy] (12.8 in n3337) in paragraph 32. It specifies that if a copy is allowed to be elided (accoring to the previous paragraph), an l-value given as copy source (in this case the name of the parameter a) is treated as r-value (i.e. can be moved from). The criteria for allowed copy elisions are given in 12.8/31, which amongst other criteria lists:
a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value
This is, in fact, the definition of the named return value optimization! And as already explained above, the named return value optimization can not work, as parameters and returned objects are located in different spaces, but now lets go back to [12.8/32] and look at the precise wording:
When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue
The part "or would be met save for the fact that the source object is a function parameter" is our life-saver here. It enables treatment as r-value even in the case where the named return value optimization is not possible just because of object storage locations and not because of semantic constraints.
EDIT: The reason I called this a corner case was missing: The life-save clause has been added very late in the C++11 standardization process, so there are some partly conforming compilers, that do not implicitly move from parameters.

Returning unique_ptr from functions

unique_ptr<T> does not allow copy construction, instead it supports move semantics. Yet, I can return a unique_ptr<T> from a function and assign the returned value to a variable.
#include <iostream>
#include <memory>
using namespace std;
unique_ptr<int> foo()
{
unique_ptr<int> p( new int(10) );
return p; // 1
//return move( p ); // 2
}
int main()
{
unique_ptr<int> p = foo();
cout << *p << endl;
return 0;
}
The code above compiles and works as intended. So how is it that line 1 doesn't invoke the copy constructor and result in compiler errors? If I had to use line 2 instead it'd make sense (using line 2 works as well, but we're not required to do so).
I know C++0x allows this exception to unique_ptr since the return value is a temporary object that will be destroyed as soon as the function exits, thus guaranteeing the uniqueness of the returned pointer. I'm curious about how this is implemented, is it special cased in the compiler or is there some other clause in the language specification that this exploits?
is there some other clause in the language specification that this exploits?
Yes, see 12.8 §34 and §35:
When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object [...]
This elision of copy/move operations, called copy elision, is permitted [...]
in a return statement in a function with a class return type, when the expression is the name of
a non-volatile automatic object with the same cv-unqualified type as the function return type [...]
When the criteria for elision of a copy operation are met and the object to be copied is designated by an lvalue,
overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue.
Just wanted to add one more point that returning by value should be the default choice here because a named value in the return statement in the worst case, i.e. without elisions in C++11, C++14 and C++17 is treated as an rvalue. So for example the following function compiles with the -fno-elide-constructors flag
std::unique_ptr<int> get_unique() {
auto ptr = std::unique_ptr<int>{new int{2}}; // <- 1
return ptr; // <- 2, moved into the to be returned unique_ptr
}
...
auto int_uptr = get_unique(); // <- 3
With the flag set on compilation there are two moves (1 and 2) happening in this function and then one move later on (3).
This is in no way specific to std::unique_ptr, but applies to any class that is movable. It's guaranteed by the language rules since you are returning by value. The compiler tries to elide copies, invokes a move constructor if it can't remove copies, calls a copy constructor if it can't move, and fails to compile if it can't copy.
If you had a function that accepts std::unique_ptr as an argument you wouldn't be able to pass p to it. You would have to explicitly invoke move constructor, but in this case you shouldn't use variable p after the call to bar().
void bar(std::unique_ptr<int> p)
{
// ...
}
int main()
{
unique_ptr<int> p = foo();
bar(p); // error, can't implicitly invoke move constructor on lvalue
bar(std::move(p)); // OK but don't use p afterwards
return 0;
}
unique_ptr doesn't have the traditional copy constructor. Instead it has a "move constructor" that uses rvalue references:
unique_ptr::unique_ptr(unique_ptr && src);
An rvalue reference (the double ampersand) will only bind to an rvalue. That's why you get an error when you try to pass an lvalue unique_ptr to a function. On the other hand, a value that is returned from a function is treated as an rvalue, so the move constructor is called automatically.
By the way, this will work correctly:
bar(unique_ptr<int>(new int(44));
The temporary unique_ptr here is an rvalue.
I think it's perfectly explained in item 25 of Scott Meyers' Effective Modern C++. Here's an excerpt:
The part of the Standard blessing the RVO goes on to say that if the conditions for the RVO are met, but compilers choose not to perform copy elision, the object being returned must be treated as an rvalue. In effect, the Standard requires that when the RVO is permitted, either copy elision takes place or std::move is implicitly applied to local objects being returned.
Here, RVO refers to return value optimization, and if the conditions for the RVO are met means returning the local object declared inside the function that you would expect to do the RVO, which is also nicely explained in item 25 of his book by referring to the standard (here the local object includes the temporary objects created by the return statement). The biggest take away from the excerpt is either copy elision takes place or std::move is implicitly applied to local objects being returned. Scott mentions in item 25 that std::move is implicitly applied when the compiler choose not to elide the copy and the programmer should not explicitly do so.
In your case, the code is clearly a candidate for RVO as it returns the local object p and the type of p is the same as the return type, which results in copy elision. And if the compiler chooses not to elide the copy, for whatever reason, std::move would've kicked in to line 1.
One thing that i didn't see in other answers is To clarify another answers that there is a difference between returning std::unique_ptr that has been created within a function, and one that has been given to that function.
The example could be like this:
class Test
{int i;};
std::unique_ptr<Test> foo1()
{
std::unique_ptr<Test> res(new Test);
return res;
}
std::unique_ptr<Test> foo2(std::unique_ptr<Test>&& t)
{
// return t; // this will produce an error!
return std::move(t);
}
//...
auto test1=foo1();
auto test2=foo2(std::unique_ptr<Test>(new Test));
I would like to mention one case where you must use std::move() otherwise it will give an error.
Case: If the return type of the function differs from the type of the local variable.
class Base { ... };
class Derived : public Base { ... };
...
std::unique_ptr<Base> Foo() {
std::unique_ptr<Derived> derived(new Derived());
return std::move(derived); //std::move() must
}
Reference: https://www.chromium.org/developers/smart-pointer-guidelines
I know it's an old question, but I think an important and clear reference is missing here.
From https://en.cppreference.com/w/cpp/language/copy_elision :
(Since C++11) In a return statement or a throw-expression, if the compiler cannot perform copy elision but the conditions for copy elision are met or would be met, except that the source is a function parameter, the compiler will attempt to use the move constructor even if the object is designated by an lvalue; see return statement for details.