为什么参考简历的格式不正确?

Why cv-qualification for a reference is ill-formed?

本文关键字:格式 不正确 参考 为什么      更新时间:2023-10-16

根据dcl.ref/1:

符合 Cv 标准的引用格式不正确,除非 cv 限定符 通过使用 typedef-name 引入

所以我试过:

int a = 1;
int& const r1 = a;

并得到错误:

error: 'const' qualifiers cannot be applied to 'int&'

预计会提示诊断消息。

但是,我想知道为什么禁止它。

只是为了防止程序员实际表示时编写上述代码的意外

const int& r1 = a; // const reference to int

主要问题

2(或者是否有一些更深层次的原因/实际例子,为什么不允许它确实是有益的?

有什么想法吗?

CV 限定符对指针有效:

int a;
int * const p = &a;

这适用于指针本身,而不是它指向的内容。

但是,由于您无法在定义后更改引用,因此将 CV-Q 添加到引用中是没有意义的。