局部变量的引用折叠
reference collapsing for local variables
在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?
是。auto
恰好被指定为模板参数推导。
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 正在折叠转发引用
- 为什么C++常量引用可以折叠为非常量引用
- 引用折叠和元组
- 模板引用折叠正在删除常量引用返回类型的cv限定符
- 为什么我们需要引用折叠规则
- C++11 完美的转发和引用折叠
- 引用与template-template类一起折叠
- 局部变量的引用折叠
- C++98/03引用折叠和cv限定符