I am looking to understand the limitations of NRVO in C++. Would a function with an initial named declaration and a single return be optimised for NRVO and elide T val
, even though the function myFunc()
has a throwing potential?
T myFunc(bool toThrow) {
T val;
if (toThrow) {
throw std::exception();
}
// Do other things here.
return val;
}
I tested a few versions of your code on C++ Insights and Compiler Explorer.
The short answer is yes, the compiler will most likely use NRVO.