Search code examples
c++c++11movestdtypename

What are the differences between `typename std::remove_reference<T>` and `constexpr typename std::remove_reference<T>`?


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)

Solution

  • [...] 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 (i.e. since ) 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

    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;
    //        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^