Vit*_*meo 13 c++ std constexpr c++14
std::exchange,在C++ 14中引入,规定如下:
Run Code Online (Sandbox Code Playgroud)template< class T, class U = T > T exchange( T& obj, U&& new_value );替换
objwith 的值new_value并返回旧值obj.
以下是cppreference的可能实现:
template<class T, class U = T>
T exchange(T& obj, U&& new_value)
{
T old_value = std::move(obj);
obj = std::forward<U>(new_value);
return old_value;
}
Run Code Online (Sandbox Code Playgroud)
据我所知,没有什么可以防止std::exchange被标记为constexpr.有没有理由我错过了为什么不能constexpr,或者这只是一个疏忽?
tam*_*bre 22
截至最新的C++ 20草案,在Albuquerque ISO C++委员会会议之后,std::exchange提出constexpr了接受提案P0202R2.