Does c++23 break unmove
Asked Answered
C

1

5

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?

Cosmogony answered 9/7, 2023 at 10:40 Comment(1)
I haven't see unmove being used even once. And the fix is simple: std::remove_reference_t<T>& vv = v; return vv;, so I'm not sure if it's worth the effort to fix.Cusack
S
9

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);
}
Shanan answered 9/7, 2023 at 11:8 Comment(1)
remove_reference_t<T>& is a very long way to spell T& :-)Derogate

© 2022 - 2024 — McMap. All rights reserved.