【问题标题】:typedef int& A can't be used as const A a = 1; [duplicate]typedef int& A 不能用作 const A a = 1; [复制]
【发布时间】: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;

【问题讨论】:

    标签: c++ c++17


    【解决方案1】:

    const int& a = 3;有什么区别

    是的。给定const AconstA 上是合格的; Aint & 的引用,并且引用本身不能被 const 限定,const 限定符被忽略。作为效果,const Aint& 相同(但不是const int&),并且int& a = 3; 格式错误,如错误消息所述。

    【讨论】:

    • 哦,我在 c++ 草稿中不太了解它,这很有帮助。谢谢!
    猜你喜欢
    • 2017-01-30
    • 1970-01-01
    • 2016-08-03
    • 2011-09-07
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 2022-12-17
    • 2012-03-06
    相关资源
    最近更新 更多