【发布时间】:2022-01-05 03:01:53
【问题描述】:
为什么不能声明为
typedef int& A;
const A a = 3;
抛出
cannot bind non-const lvalue reference of type 'A' {aka 'int&'} to an rvalue of type 'int'
这两者有什么区别吗
const int& a = 3;
【问题讨论】:
为什么不能声明为
typedef int& A;
const A a = 3;
抛出
cannot bind non-const lvalue reference of type 'A' {aka 'int&'} to an rvalue of type 'int'
这两者有什么区别吗
const int& a = 3;
【问题讨论】:
和
const int& a = 3;有什么区别
是的。给定const A,const 在A 上是合格的; A 是 int & 的引用,并且引用本身不能被 const 限定,const 限定符被忽略。作为效果,const A 与int& 相同(但不是const int&),并且int& a = 3; 格式错误,如错误消息所述。
【讨论】: