I’m trying to create a WCHAR:
LONG bufferSize = foo.bar() + 1;
WCHAR wszBaz[bufferSize];
The compiler issues an error:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'wszBaz' unknown size
What am I doing wrong?
UPDATE: I added const but it still gives the same error:
const LONG bufferSize = foo.bar() + 1;
WCHAR wszBaz[bufferSize];
Array sizes must be constant expression:
Note that
constdoes not make something a const expression. A const expression is something that is constant at compile time. The compiler is smart enough to figure out that something like5+5is a const expression, but isn’t smart enough to figure out thatfoo(5,5)is a const expression — even iffoo(x,y)just returnsx+y.In the next C++ standard (C++0x), you will be able to define functions as const-expressions.