逗号运营商在声明中
The comma operator in a declaration
我正在编写C 解析器(实际上是其中的一小部分),并且找不到有关为什么在可变初始化中不允许逗号运算符的解释。
int a = 1, 2; // throws a "Expected ';' at the end of declaration" compiler error
a = 1, 2; // assigns the result of the comma binary operator to the a (2)
int a = (1, 2); // does the same as above, because paren expression is allowed as an initializer
C 规格说您可以将表达式用作变量声明中的初始化器。为什么不允许逗号二进制表达,但是允许所有其他表达式(具有更高优先级)?cppreference.com(http://en.cppreference.com/w/cpp/language/initialization)说,任何表达式都可以用作初始化器。
C 规格的第8.5节说,初始化器只能包含分配表达。这是调节分配是初始化中允许的最低特定表达式的位置吗?
语言语法将初始化器中的逗号解释为逗号删除的宣言子句,即形式:
int i = 2, j = 3;
要避免这种歧义,您需要在括号中包裹逗号。
来自 [dcl.decl] :
[...]
init-declarator-list:
init-declarator
init-declarator-list , init-declarator
[...]
相关文章:
- 呼叫运营商<<临时
- 两个运营商的一些奇怪的冲突<<
- 如何在 CPP 中访问家长的运营商
- 如何明确调用好友流运营商
- 私有运营商删除会触发 GCC 和 Clang 的编译时错误,但不会在 MSVC 上触发编译时错误
- 使用运营商New分配的数据结构是否有任何副作用
- "Inheriting"移动运营商?
- 朋友ostream&运营商<<无法访问私人会员
- 为什么在下面的代码返回类型中是用于运营商重载的类类型
- 范围的枚举(枚举类)关系运营商
- 为什么“操作员”需要const但不是为“运营商&lt;”
- 对这两个分配运营商之间的不同感到困惑
- 我的班级意外加法运营商
- OpenACC - C++"新"运营商问题
- c 对运营商的一致性是新的,有多少重要
- 为什么我不能使私人运营商成为新的并使用默认实现?
- 全球取代所有新运营商
- 包装C 朋友在Cython中的非会员运营商
- 逗号运营商在声明中
- 朋友向成员操作员*的模板运营商*声明*