1. 31
    1. 4

      clang-tidy catches the mentioned case which disables NRVO, and using it is super helpful getting RVO and a lot of other things correct.

      1. 3

        GCC 14 recently has a warning for that built in.

      2. 2

        In C++17, NRVO is always guaranteed to happen. See https://en.cppreference.com/w/cpp/language/copy_elision

        1. 4

          Copy elision and NRVO are not exactly the same thing. NRVO is not guaranteed, and it is an area GCC and Clang have been improving at much over the past year. Here is a list of improvements that could be made in the future to guarantee it in more places:

          https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2025r2.html

        2. 1

          There’s a really annoying corner case in guaranteed copy elision: the copy or move constructor must exist. In a few cases, I want to generate guards that are bound to the enclosing lexical scope and can’t be copied or moved. With NRVO, it’s visible in the abstract machine that these are not ever copied or moved, but if I delete both copy and move constructors then the compile fails. I hoped C++20 would fix that.

          1. 1

            I am guessing there is a practical reason, but why not just move the logic out of the freestanding function and into the guard class’ constructor? To handle failure without exceptions?

          2. 1

            (commonly known as Named Return Value Optimization, or NVRO)

            […]

            The added std::move prevents NVRO

            Minor typo, it’s NRVO.