4

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;
}
Jørgen Fogh
  • 7,516
  • 2
  • 36
  • 46
Doggiie
  • 95
  • 1
  • 1
  • 4

1 Answers1

1

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.

Jørgen Fogh
  • 7,516
  • 2
  • 36
  • 46