是什么原因`std :: exchange`不是`constexpr`?

时间:2017-11-12 11:57:19

标签: c++ c++14 std constexpr

在C ++ 14中引入的

std::exchange规定如下:

template< class T, class U = T >
T exchange( T& obj, U&& new_value );
     

obj的值替换为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;
}

据我所见,没有什么能阻止std::exchange被标记为constexpr有理由我错过了为什么它不能constexpr,或者这只是一个疏忽?

1 个答案:

答案 0 :(得分:22)

从最新的C ++ 20草案开始,在the Albuquerque ISO C++ committee meeting之后,std::exchange成为constexpr并接受the proposal P0202R2