I can write operator new for one-dimensional array as follows:
sizeof(int) * n
new int[n]; //ok
new int[n][n]; //error;
sizeof(int) * n * n
The C++ type system does not include arrays with runtime bound. This is a very complicated thing to do, considering that it will have implications for templates and overload resolution. There have been proposals but none has progressed to being accepted for standardization.
T[n] is not a valid type. However it can be used in a
new-expression because there is a special case for it. The
new-expression can be either:
new X, where
Xis a type
new T[n], where
Tis a type and
nis not a constant expression.
Note that both cases are needed because
T[n] is not a type but we want to allow that in a
The second point needs a little bit more explanation. It actually uses the C++ infix notation, so if
T is an array or function type, the
[n] will be in a different place. For example
new int[n] is OK , which is the same as
typedef int T; new T[n]. But
new int[n] is not.
If we did allow
new int[n], what would the return type be?
int (*)[n] is not part of the C++ type system as mentioned earlier.