Aug 6, 2023 at 12:37am
If
t is a forwarding reference of type
T,
static_cast<T&&>(t) is equivalent to
std::forward<T>(t).
In this example
_Val and
_New_val are forwarding references, so the implementation is equivalent to
1 2 3 4 5 6 7 8
|
EXPORT_STD template <class _Ty, class _Other = _Ty>
_CONSTEXPR20 _Ty exchange(_Ty& _Val, _Other&& _New_val) noexcept(
conjunction_v<is_nothrow_move_constructible<_Ty>, is_nothrow_assignable<_Ty&, _Other>>) {
// assign _New_val to _Val, return previous _Val
_Ty _Old_val = std::forward<_Ty>(_Val);
_Val = std::forward<_Other>(_New_val);
return _Old_val;
}
|
This is sometimes done in library code to avoid depending on
std::forward.
Last edited on Aug 6, 2023 at 12:37am
Aug 6, 2023 at 5:02pm
but _Val is not a forwarding reference; it is a simple reference of type _Ty&...
so static_cast<_Ty&&>(_Val) is equivalent to std::move(_Val)
Am I wrong?
Aug 6, 2023 at 6:14pm
No, you're right. I misread.