gpt4 book ai didi

c++ - 为什么引用的 cv-qualification 格式错误?

转载 作者:太空狗 更新时间:2023-10-29 20:34:10 26 4
gpt4 key购买 nike

根据 dcl.ref/1 :

Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a 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 毫无意义。

关于c++ - 为什么引用的 cv-qualification 格式错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50322713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com