1

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)
JeJo
  • 30,635
  • 6
  • 49
  • 88
sunshilong369
  • 646
  • 1
  • 5
  • 17

1 Answers1

3

[...] 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;
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • Why the former function signature does not need the keyword `typename`?Thank you. – sunshilong369 May 28 '20 at 05:33
  • @sunshilong369 The resultant type of traits `std::remove_reference` depends on the template type `T`, which needed to be mentioned to the compiler by `typename` keyword. See more examples here: [When is the “typename” keyword necessary?](https://stackoverflow.com/questions/7923369/when-is-the-typename-keyword-necessary) – JeJo May 28 '20 at 05:37
  • You see, there is `std::remove_reference` in former function signature, too.So why there is no need to put `typename` before it? – sunshilong369 May 28 '20 at 05:42
  • 1
    Sorry.My eyes deceive me.There are typename keywords in both of them indeed.My heart is full of thanks. – sunshilong369 May 28 '20 at 05:52