Quoting the C++11 standard (12.8/31), "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.", so:
- Copy elision is not guaranteed. (technically; but compiler vendors are motivated to so for marketing reasons)
- This is a compiler-specific question.
- Technically there are some cases (see here) but they would indicate (IMO) some error in the design.
You should still provide move constructor (if it is possible to implement it effectively). There are some cases where copy elision is prohibited but move constructor is just fine:
vector<string> reverse(vector<string> vec)
{
reverse( vec.begin(), vec.end() );
return vec;
}
auto vec = reverse(vector<string>{"abc", "def", "ghi"});
Copy elision is explicitly not allowed to propagate from function argument to the final automatic value initialized from a temporary. But no copy is called due to move constructor.
To be more specific about my last remark, I quote N3290, which is difficult to get nowadays, but it is very close to N3337:
12.8/31:
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):
-- in 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
12.8/32:
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. If overload resolution fails, or if the type of the first parameter of the selected constructor is not an rvalue reference to the object’s type (possibly cv-qualified), overload resolution is performed again, considering the object as an lvalue. [...]