In C++17, Return Value Optimization (RVO) became a language guarantee rather than an optional compiler optimization:
std::vector<int> MakeVector() {
return std::vector<int>(1000000, 42); // Guaranteed RVO (No copy, no move!)
}
std::vector<int> v = MakeVector();The compiler constructs the vector directly inside the storage location allocated for v on the caller's stack frame. Neither the copy constructor nor the move constructor is ever invoked!