The easy part: Does it return "a" by reference or by value?
A find()
{
...
A a;
return a;
}
Returns by value.
The hard part: Moreover, does "find" delete "a" first, then return or return first, then delete "a"?
Technically a copy of a
is constructed, a
is destroyed and the copy is returned. I cannot find anything in the C++ standard that specifies any particular ordering to those operations, but some are logically implied. Obviously you cannot copy after destruction.
I suspect this is left unspecified to allow C++ implementations to support a wide variety of calling conventions.
Note: This means the returned object must be copy-able. If the copy constructor is deleted or inaccessible, you cannot return by value.
There is no way to be certain of whether the return or the destruction is first. It should not matter and if you are designing a program where it does, give your head a shake.
Caveat
However in practice a modern optimizing compiler will do anything in its power to avoid copying and destroying using a variety of approaches under the blanket name of Return Value Optimization.
Note that this is a rare case where the As-If Rule is allowed to be violated. By skipping the copy construction and destruction, some side effects may not take place.
Also note that even if the need to copy is eliminated, the object must still be copy-able.
Sidenote:
A & find()
{
...
A a;
return a;
}
will return a reference, but this is a very bad idea. a
has Automatic storage duration scoped by the function and will be destroyed on return. This leaves the caller with a dangling reference, a reference to a variable that no longer exists.
To get around this,
std::unique_ptr<A> find()
{
...
auto a = std::make_unique<A>();
return a;
}
but you will find that, with a modern compiler, this is no better than returning by value in most cases.