Recently when answering a question I realized that the comma operator is allowed in a constant expression in C++11 as long as the expression is surrounded by ()
, for example:
int a[ (1, 2) ] ;
Pre C++11 it is forbidden to use the comma operator in a constant expression, from the draft pre C++11 standard section 5.19
Constant expressions which says (emphasis mine):
[...]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.
Why was the comma operator not allowed in a constant expression pre C++11 and why was this restriction lifted?
We can find the answer to this in the std-discussion group Comma operator in constant-expression thread where Gabriel Dos Reis says:
and Richard Smith earlier in the thread notes some of the uses of the comma operator in a constant expression in both C++11 and C++14:
Note it was argued that his C++11 example is not correct since the the expression containing the comma operator should be in
()
but his example gives the essence of the argument. The argument would be based on the grammar from section5.19
Constant expressions:we can not get to comma operator from a conditional-expression but we can get to primary-expression which gets us to
( expression )
and we can then get to the comma operator from expression.T.C. points out that this may not be the case since the relevant section seems vague on this point.