简体   繁体   English

`typename std::remove_reference 之间有什么区别<t> ` 和 `constexpr 类型名 std::remove_reference<t> `?</t></t>

[英]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.根据文档( https://en.cppreference.com/w/cpp/utility/move ), std::move<T>有两种构造函数,发布在下面。

What are the differences between these constructors?这些构造函数之间有什么区别? What confused me most is that why there needs the keyword( typename ) in the second constructor.最让我困惑的是为什么在第二个构造函数中需要关键字( typename )。

I am a novice in C++.我是 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> ... [...] std::move<T>有两种构造函数...

No, they are not constructors, rather function signatures of std::move .不,它们不是构造函数,而是std::move的 function 签名。 One is prior to (ie since ) and the second one since C++14.一个在之前(即从),第二个在 C++14 之后。

In the second one the specifier constexpr is used , meaning在第二个中使用了说明符constexpr ,意思是

constexpr - specifies that the value of a variable or function can appear in constant expressions constexpr - 指定变量或 function 的值可以出现在常量表达式中

read more here: What are 'constexpr' useful for?在此处阅读更多内容: “constexpr”有什么用?


What confused me most is that why there needs the keyword( typename ) in the second constructor.最让我困惑的是为什么在第二个构造函数中需要关键字( typename )。

As per the cppreference.com , there is a helper type for std::remove_reference , since 根据cppreference.comstd::remove_reference有一个辅助类型,因为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM