Search⌘ K
AI Features

Named Return Value Optimization

Explore Named Return Value Optimization in C++17 to understand how modern compilers optimize return values by eliminating unnecessary copies, improving performance, and following new clear rules on object elision.

Compilers and NRVO

Compilers are even smarter, and they can el​ide in cases when you return a named object - it’s called Named Return Value Optimization - NRVO

C++
Test Create()
{
Test t;
// several instruction to initialize 't'...
return t;
}
auto n = Create(); // temporary will be usually elided
...