I want to do something like:
const int N = 10;
void foo (const int count)
{
int (* pA) [N][count] = reinterpret_cast<int(*)[N][count]>(new int[N * count]);
...
}
But my compiler (VS2010) doesn't want to do it:
error C2057: expected constant expression
error C2540: non-constant expression as array bound
Such a way he expresses his dissatisfaction with the count.
I know, how it can be worked around by implementing a slightly different way. But I just don't get, why does C++ forbid me to use this way. I understand, why C++ needs to know on-stack arrays size at compile time (to allocate array memory). But why the same restrictions are necessary with respect to pointers to array (after all, pointer is just a tool to work with that allocated memory)?
constis wildly insufficient for guaranteeing compile-time knowability (and, therefore, use as an array bound).countto bound the actual data is not enough. Even just trying to use it as part of a type (as you are doing here) is already impossible. Types are a compile-time construct, yet the expressioncountis meaningless until run-time. So how is that supposed to work?