1. Elements of constant array as constant expressions.
C/C++ language proposal:
Change the 'case expression' from "integral constant-expression" to "integral expression"
The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) [see also 5.19]:
case constant-expression :
I propose that the case expression of the switch statement
be changed from "integral constant-expression" to "integral expression".
This opens up many new possibilities since then also function calls
would be permitted in the case expression.
The old case case would continue to function since
it is a subset of the new case case.
Example usage:
//...
int f()
{
//...
return BLA1;
}
int g()
{
//...
return BLA2;
}
int h()
{
//...
return BLA3;
}
int y, x = f();
switch (x)
{
case 123 : y = g(); break;
case g() : y = 456; break; // using new case feature, ie. func-call
case h() : y = 789; break; // ditto
default : y = -1; break;
}
4. Array size as constant expression
5. size of array is not an integral constant-expression
#include <vector>
using namespace std;
template <typename Iter>
int
foo(Iter first, Iter last, int nn)
{
const size_t n = last - first;
double buf[n];
return 0;
}
int
main(int argc, char **argv)
{
vector<double> x;
foo(x.begin(), x.end(), argc);
return 0;
}
foo.cc:17: instantiated from here
foo.cc:9: error: size of array is not an integral constant-expression
g++ 4.2.1
Is this error specific to g++ 4.x? g++ 3.6.4 and g++ 2.9.5 have no
problems with it, but that doesn't mean they are right. Is there some
reason to expect this to fail.
There are a few interesting workarounds that point to this being
unexpected behavior... I'll post those next.
6. an array reference cannot appear in a constant-expression
7. declaration of array with non-constant expression?
Hi Everyone, I've just come across an expression that I always thought should not compile: int j = 0; int b[j]; But it compiles fine under gcc4.3.4. Obviously it is not very useful, but what does it mean? No memory can be allocated at compile time, but b seems to act like a pointer. I've browsed through the standard and found nothing that would indicate that this is legal. The section on declarations of arrays (8.3.4) says that only constant expressions are allowed in brackets. Can anyone please explain this to me? Thank you. -Mike