【问题标题】:C++ extract actual type from universal referenceC++ 从通用引用中提取实际类型
【发布时间】:2020-10-16 11:31:38
【问题描述】:

我怎样才能像这样提取通用引用的实际类型:

template<class T>
void foo(T&& value) {
    otherFunction<...>(value);

我想将... 替换为值的实际类型,即既不是T&amp;T&amp;&amp; 也不是const T&amp;,而只是T

另外,我真的需要std::forward 值吗?因为otherFunction 只接受实际值,所以无论如何它都会丢弃任何类型的引用。那么我不需要std::forward 是否正确?

【问题讨论】:

标签: c++ reference c++17 forwarding-reference


【解决方案1】:

使用std::remove_reference。它将把intint&amp;int&amp;&amp; 变成int

【讨论】:

  • 准确地说是typename std::remove_reference&lt;T&gt;::type,或者std::remove_reference_t&lt;T&gt;
  • 您忘记删除 cv 限定符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
  • 1970-01-01
  • 2016-12-18
  • 2018-12-01
  • 1970-01-01
相关资源
最近更新 更多