Search code examples
c++language-lawyerc++23

Does c++23 break unmove


The wording in the standard regarding implicit move changed in c++23
see https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2266r3.html
relevant part is:

a returned move-eligible id-expression is always an xvalue

this seems to break the popular but unwise 'unmove'

template<typename T>
auto unmove(T&& v) -> std::remove_reference_t<T>& { return v; }

Current implementations seem to agree: https://godbolt.org/z/3n39rGM7b

Is this breaking change intended, or can we expect a DR?


Solution

  • Since this breakage is mentioned in the Implementation experience section of P2266R3, I'd say it's quite obviously intentional.

    The fix is simple: just add an explicit cast.

    template<typename T>
    auto unmove(T&& v) -> std::remove_reference_t<T>& {
        return static_cast<std::remove_reference_t<T>&>(v);
    }