This explanation of copy elision states that
Under the following circumstances, the compilers are required to omit the copy and move construction of class objects, even if the copy/move constructor and the destructor have observable side-effects. The objects are constructed directly into the storage where they would otherwise be copied/moved to. The copy/move constructors need not be present or accessible, as the language rules ensure that no copy/move operation takes place, even conceptually:
In a return statement, when the operand is a prvalue of the same class type (ignoring cv-qualification) as the function return type:
T f() { return T(); }
f(); // only one call to default constructor of T
My question is why does the following code not compile then:
#include <mutex> std::mutex createMutex() { return std::mutex(); } int main() { auto mutex = createMutex(); }