As per the documentation(https://en.cppreference.com/w/cpp/utility/move), there are two kinds of constructors for std::move<T>
, which are posted below.
What are the differences between these constructors?
What confused me most is that why there needs the keyword(typename
) in the second constructor.
I am a novice in C++. I would be thankful for any hint on this question.
template< class T >
typename std::remove_reference<T>::type&& move( T&& t ) noexcept; (since C++11)(until C++14)
template< class T >
constexpr typename std::remove_reference<T>::type&& move( T&& t ) noexcept; (since C++14)
[...] there are two kinds of constructors for
std::move<T>
...
No, they are not constructors, rather function signatures of std::move
. One is prior to c++14(i.e. since c++11) and the second one since C++14.
In the second one the specifier constexpr
is used, meaning
constexpr
- specifies that the value of a variable or function can appear in constant expressions
read more here:What are 'constexpr' useful for?
What confused me most is that why there needs the keyword(
typename
) in the second constructor.
As per the cppreference.com, there is a helper type for std::remove_reference
, since c++14
template< class T >
using remove_reference_t = typename remove_reference<T>::type; (since C++14)
therefore in the second one, it could have been
template< class T >
constexpr std::remove_reference_t<T>&& move( T&& t ) noexcept;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^