I can have
std::bitset< 10 > bitsetA;
or
const size_t LengthB = 20;
std::bitset< LengthB > bitsetB;
without any problem.
But, if the length is not const
size_t LengthC = 30;
std::bitset< LengthC > bitsetC; // Line 30, say
I face the following compilation error
'LengthC' cannot appear in a constant-expression
template argument 1 is invalid
What is the reason for that?
What would be the problem, for compiler and for user code, if line 30 was to be accepted? Is it because LengthC might have some alias?
Template arguments have to be declared
constat compile time so that the template can be instantiated at compile time.In the example you give, it does indeed appear that
LengthCisn’t going to change from the point where it is initialized to the point where the template has to be instantiated, so it could be treated as constant, but the compiler is not obligated to figure that out. The spec says the arguments have to be declaredconst, so that no compile-time flow control analysis needs to be done.