Chapter 2.3.2 of The C++ Programming Language lists this constructor:
class Vector {
public:
Vector(int s) :elem{new double[s]}, sz{s} { }
private:
double* elem;
int sz;
};
As far as I know, the array size must be a constant expression, and s isn't. Is this legal? If so, why?
new[]was invented in the first place. A *declared * array size must be a constant, butnewis not a declaration.newexpression is not required to be a constant expression. Also, in your code,elemis a pointer, not an array.