Hi I am putting my hands on C++11 and there is a behavior I am not able to understand:
std::unique_ptr<int> foo()
{
std::unique_ptr<int> p(new int(3));
return p; //1
}
int main()
{
std::unique_ptr<int> p2 = foo(); //2
}
Compile
std::unique_ptr<int> p(new int(3));
std::unique_ptr<int> foo()
{
return p;
}
int main()
{
std::unique_ptr<int> p2 = foo(); //2
}
Don't compile saying that I am trying to use a deleted operation (probably copy), but both //1 and //2 are making copy of the object which is not allowed.
I read the standard:
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 […]
But it is not really clear to me. Thanks for your help.