引用到引用在C++中是什么意思?(不是右值引用)
What does a reference-to-reference mean in C++? (Not an rvalue reference)
>假设我有
typedef int& intr;
typedef intr& intrr;
我可以声明
int x = 7;
intrr y = x;
但什么是引用到引用?intrr在语义上与intr有什么不同吗?
没有引用这样的东西。C++标准明确指出:
§8.3.3/5
不得引用参考文献,...
在 typedefs 和模板中,有一个规则通常称为"引用折叠"。同一部分的第 6 段对此进行了描述:
如果是 typedef (7.1.3)、类型模板参数 (14.3.1) 或 decltype-specifier (7.1.6.2) 表示一个类型
TR
,它是对类型T
的引用,尝试创建类型"对 cv 的左值引用"TR
"创建类型"对T
的左值引用",同时尝试 创建类型"对 CVTR
的右值引用"创建类型TR
。[ 示例:
int i; typedef int& LRI; typedef int&& RRI; LRI& r1 = i; // r1 has the type int& const LRI& r2 = i; // r2 has the type int& const LRI&& r3 = i; // r3 has the type int& RRI& r4 = i; // r4 has the type int& RRI&& r5 = 5; // r5 has the type int&& decltype(r2)& r6 = i; // r6 has the type int& decltype(r2)&& r7 = i; // r7 has the type int&
— 结束示例 ]
样本中的intr
和intrr
是完全相同的类型,即int&
。
#include <iostream>
#include <type_traits>
int main()
{
typedef int& intr;
typedef intr& intrr;
int x = 7;
intrr y = x;
std::cout << std::is_same<intr, intrr>::value;
std::cout << std::is_same<int&, intrr>::value;
std::cout << std::is_same<int&, intr>::value;
}
输出:111
托马斯·贝克尔(Thomas Becker)关于右值参考文献的文章解释了一个很好的表格:
- A& & 成为 A&
- A& && 成为 A&
- A&& 成为 A&
- A&
& && 成为 A&&
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- 错误:无法解析对重载函数的引用;你的意思是调用它吗?
- 通过引用函数传递指针参数是什么意思?
- 对struct::struct的未定义引用是什么意思
- (.text+0x20):对"主要"的未定义引用是什么意思
- 引用 using 声明引入的功能的句子是什么意思?
- 引用的无效初始化是什么意思
- lock_guard始终拥有引用互斥锁的锁定模式是什么意思
- 引用到引用在C++中是什么意思?(不是右值引用)
- "bind directly"在引用初始化中是什么意思?
- 左值引用类型是什么意思
- 模板类型的引用是什么意思?