为什么在 C++ 中没有像 const 指针那样的 const 引用?
Why no const reference in C++ just like const pointer?
int main()
{
int n = 1;
int* const p = &n; // ok
*p = 2; // ok as expected.
p = 0; // error as expected.
int& const m = n;
// error: 'const' qualifier may not be
// applied to a reference
return 0;
}
为什么 C++ 中没有像 const 指针一样的 const 引用?
设计背后的基本原理是什么?
Why no const reference in C++ just like const pointer?
无法修改引用。向不可修改的实体添加 const 限定将毫无意义且令人困惑。
请注意,从技术上讲,可以通过类型别名或模板类型参数间接将 const 应用于引用。示例:
T some_t;
using Ref = T&;
Ref const some_ref = some_t; // well-formed
Ref const
将"collapses"键入T&
,与不合格的Ref
相同。我建议通常避免为指针和引用创建类型别名,除非它们是常规的极少数情况。具体来说,Container::reference
类型别名和类似的是常规的。
C++ 中的 References 在几个基本方面不同于指针。区别之一是:
Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.
这意味着 Reference
与 const 指针 相似 相似(参见本答案末尾的 link)( 不在 C++ 中指向 const!) 的指针...
int a = 5;
int& m = a; // Behaves similar to int * const m = &a;
// See the link at the bottom for the differences between const pointer and reference.
因此,您不能 change/rebind 它们指向其他地址。因此,您不需要明确的 const
限定符作为引用,这就是编译器不允许它的原因。
查看此 link 以了解 Why are references not reseatable in C++?
。我已经复制了上面接受的答案 link:
Stroustrup 的 "Design and Evolution of C++" 中给出了 C++ 不允许重新绑定引用的原因:
It is not possible to change what a reference refers to after initialization. That is, once a C++ reference is initialized it cannot be made to refer to a different object later; it cannot be re-bound. I had in the past been bitten by Algol68 references where r1=r2 can either assign through r1 to the object referred to or assign a new reference value to r1 (re-binding r1) depending on the type of r2. I wanted to avoid such problems in C++.
编辑:
参见 this link Difference between const pointer and reference?
(感谢@M.M 指出了我陈述中的歧义)。
int& const m = n;
恕我直言,因为编译器本质上它是不变的,只是
int n ;
n
具有固有的常量引用
因此,因为它是解析代码,所以它只是确定只允许 const 限定符存在的位置,通过编译器规则方法进行解析,如果不允许则转到 error/warning
int main()
{
int n = 1;
int* const p = &n; // ok
*p = 2; // ok as expected.
p = 0; // error as expected.
int& const m = n;
// error: 'const' qualifier may not be
// applied to a reference
return 0;
}
为什么 C++ 中没有像 const 指针一样的 const 引用?
设计背后的基本原理是什么?
Why no const reference in C++ just like const pointer?
无法修改引用。向不可修改的实体添加 const 限定将毫无意义且令人困惑。
请注意,从技术上讲,可以通过类型别名或模板类型参数间接将 const 应用于引用。示例:
T some_t;
using Ref = T&;
Ref const some_ref = some_t; // well-formed
Ref const
将"collapses"键入T&
,与不合格的Ref
相同。我建议通常避免为指针和引用创建类型别名,除非它们是常规的极少数情况。具体来说,Container::reference
类型别名和类似的是常规的。
References 在几个基本方面不同于指针。区别之一是:
Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.
这意味着 Reference
与 const 指针 相似 相似(参见本答案末尾的 link)( 不在 C++ 中指向 const!) 的指针...
int a = 5;
int& m = a; // Behaves similar to int * const m = &a;
// See the link at the bottom for the differences between const pointer and reference.
因此,您不能 change/rebind 它们指向其他地址。因此,您不需要明确的 const
限定符作为引用,这就是编译器不允许它的原因。
查看此 link 以了解 Why are references not reseatable in C++?
。我已经复制了上面接受的答案 link:
Stroustrup 的 "Design and Evolution of C++" 中给出了 C++ 不允许重新绑定引用的原因:
It is not possible to change what a reference refers to after initialization. That is, once a C++ reference is initialized it cannot be made to refer to a different object later; it cannot be re-bound. I had in the past been bitten by Algol68 references where r1=r2 can either assign through r1 to the object referred to or assign a new reference value to r1 (re-binding r1) depending on the type of r2. I wanted to avoid such problems in C++.
编辑:
参见 this link Difference between const pointer and reference?
(感谢@M.M 指出了我陈述中的歧义)。
int& const m = n;
恕我直言,因为编译器本质上它是不变的,只是
int n ;
n
具有固有的常量引用
因此,因为它是解析代码,所以它只是确定只允许 const 限定符存在的位置,通过编译器规则方法进行解析,如果不允许则转到 error/warning