12 ms·
That's sort of what happens with named return value optimization. You write this: bar foo() {bar baz; ...; return baz;} bar qux = foo(); 'baz' is constructed
by immibis 2y ago
That's sort of what happens with named return value optimization. You write this:
bar foo() {bar baz; ...; return baz;}
bar qux = foo();
'baz' is constructed directly in the space allocated for 'qux'.
Implementation notes: the complex value is never returned but rather the caller passes the address of a space where the return value should be constructed. Inside the function, the compiler notes that baz is returned and allocates it in the return value space.
This optimization is guaranteed to occur. Compilers which don't do this are nonconforming.
- Joker_vD 2y ago> This optimization is guaranteed to occur. I vaguely remember that there are some obscure corner cases when it's won't occur but I can't recall them since I haven't written C++ since 2016.
- Arnavion 2y ago>This optimization is guaranteed to occur. Compilers which don't do this are nonconforming. NRVO is not guaranteed to occur, only unnamed RVO (`bar foo() { return bar(...) ; }`) is.