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.