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) 

1 Answer 1

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; // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
Sign up to request clarification or add additional context in comments.

4 Comments

Why the former function signature does not need the keyword typename?Thank you.
@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?
You see, there is std::remove_reference in former function signature, too.So why there is no need to put typename before it?
Sorry.My eyes deceive me.There are typename keywords in both of them indeed.My heart is full of thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.