【问题标题】:reference collapsing for local variables局部变量的引用折叠
【发布时间】:2011-08-11 09:39:39
【问题描述】:

在以T 参数化的函数模板中,T&& 类型可能是也可能不是右值引用,具体取决于参数的值类别:

template <typename T>
void function(T&& x)
{
    // ...
}

std::string some_named_string;
function(some_named_string);       // T&& is std::string&

function(std::string("hello"));    // T&& is std::string&&

同样的规则是否也适用于自动推断类型的局部变量?

auto&& x = some_named_string;      // is x a std::string& here?
auto&& y = std::string("hello");   // is y a std::string&& here?

【问题讨论】:

    标签: c++ templates c++11 type-inference rvalue-reference


    【解决方案1】:

    是的。 auto 完全指定为模板参数推导。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 2011-11-06
      • 2018-01-12
      相关资源
      最近更新 更多