逗号运算符是否允许在C ++ 11中的常量表达式中? [英] Is the comma operator allowed in a constant-expression in C++11?

查看:359
本文介绍了逗号运算符是否允许在C ++ 11中的常量表达式中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回答这个问题对于C ++ 11的SO ,我意识到在C ++ 03(以及C)中,逗号运算符的使用在常量中被明确禁止-expression



C ++ 03标准关于常量表达式的第5.19 / 1段说:


[...]特别是,除了sizeof表达式,函数,类对象,指针或
引用之外,不应使用赋值,递增,递减,


在C ++ 11中,然而,最后一部分提到逗号运算符似乎消失了。虽然C ++ 11标准的第5.19 / 2段明确规定赋值,增量,减量和非 - constexpr 函数调用表达式不应作为子表达式出现



例如,下面的程序编译了一个常量表达式的常量表达式 GCC 4.7.2和Clang 3.3与 std = c ++ 11 (除了编​​译器警告说逗号运算符没有效果和 x arr 变量未使用):

  int main()
{
constexpr int x =(0,42);
int arr [(0,42)];
}

然而,必须说,即使下面的程序编译精细, code> -std = c ++ 03 选项(在Clang和GCC上),这显然不是正确的, 03标准:

  int main()
{
int arr [(0,42)
}

QUESTION: b

C ++ 03和C ++ 11之间在常量表达式中是否允许逗号运算符是有区别的,还是我缺少一些东西?



作为一个奖励(非建设性)问题,我有兴趣知道为什么逗号运算符不能在C ++ 03中的常量表达式中使用。

解决方案


  1. 是的,我相信这是C ++ 03和C ++ 11。我相信这是为了大致说明你所提到的原因 - 没有特别好的理由逗号运算符不能作为常量表达式的一部分。


  2. p>我相信C ++ 03中的规则源自C(C90,§6.4)中的规则:


常量表达式不应包含赋值,增量,减量,函数调用或逗号运算符,除非它们包含在 sizeof 运算符的操作数中。


至于为什么C中的常量表达式禁止逗号运算符,我只能推测。我的直接猜测是确保一个定义,如:

  int x [2,5] 

...将被拒绝,而不是让用户错误地相信他定义了2x5元素数组,当(如果允许逗号操作符)他真正定义了 x 只有5个元素。


In the process of answering this question on SO for C++11, I realized that in C++03 (as well as in C) the use of the comma operator is explicitly forbidden in a constant-expression.

Paragraph 5.19/1 of the C++03 Standard on constant expressions says:

[...] In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall not be used.

In C++11, however, that last part mentioning the comma operator seems to be vanished. And while paragraph 5.19/2 of the C++11 Standard clearly specifies that assignment, increment, decrement, and non-constexpr function call expressions shall not appear as sub-expressions of a constant-expression, the usage of the comma operator does not seem to be forbidden anymore.

For instance, the following program compiles fine on GCC 4.7.2 and Clang 3.3 with std=c++11 (apart from compiler warnings saying the comma operator has no effect and the x and arr variables are unused):

int main()
{
    constexpr int x = (0, 42);
    int arr[(0, 42)];
}

However, it must be said that even the following program compiles fine with the -std=c++03 option (both on Clang and GCC), which is clearly not correct, given the above quote from the C++03 Standard:

int main()
{
    int arr[(0, 42)];
}

QUESTION:

Is there a difference between C++03 and C++11 as to whether or not the comma operator is allowed in a constant expression, or am I missing something?

As a bonus (non-constructive) question, I would be interested in knowing why the comma operator cannot be used in a constant expression in C++03.

解决方案

  1. Yes, I believe this is a change between C++03 and C++11. I believe it was done for roughly the reason to which you allude -- that there's no particularly good reason a comma operator can't be part of a constant expression.

  2. I believe the rule in C++03 originated from the rule in C (C90, §6.4):

Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within the operand of a sizeof operator.

As to why the comma operator was prohibited in constant expressions in C, I can only speculate. My immediate guess would be to assure that a definition like:

int x[2, 5];

...would be rejected instead of leaving the user with the mistaken belief that he'd defined a 2x5 element array, when (if a comma operator were allowed there) he'd really defined x with only 5 elements.

这篇关于逗号运算符是否允许在C ++ 11中的常量表达式中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆