gpt4 book ai didi

C++ 如何在单个定义中的多个声明符中定义 const 引用?

转载 作者:行者123 更新时间:2023-11-30 02:37:01 25 4
gpt4 key购买 nike

基本形式:

1. int x = 1, &r = x; -> good    
2. int x = 1;
const int &r = x; -> good
3. int x = 1;
int const &r = x; -> good
4. int x, const int &r = x; -> failed
5. int x, const &r = x; -> failed

问题:
1) 两者之间有什么不同:

const int &r = x and
int const &r = x

似乎没有区别。

2) 如何使用 4) 和 5) 中的单一定义将 const 引用与其他 int 声明符一起编写

最佳答案

const int &r = xint const &r = x 没有区别。

您尝试执行的操作是不可能的。声明包括:

  1. decl-specifier-seq,它是 decl-specifiers 的序列,例如 intconst。在大多数情况下,decl-specifiersdecl-specifier-seq 中的顺序无关紧要。
  2. 一个 init-declarator-list, 这是(不出所料)init-declarators 的列表。 init-declarator 可以是 x&r = x

通过将 each init-declaratorevery decl-specifier 单独组合来解释声明。因此,在单个声明中声明的所有实体都有一个共同的 decl-specifier-seq。由于 const 是一个decl-specifier 而不是声明符的一部分,您不能让它仅应用于声明中的一个声明符。

关于C++ 如何在单个定义中的多个声明符中定义 const 引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32084181/

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