I have a template class from an API that is instantiated with something like this.
BitField< length > object;
The problem is that length variable is only known at runtime.
error: ‘length’ cannot appear in a constant-expression -> this is the error message
Any suggestions ?
Templates are strictly a compile-time concept. After compilation, they’re baked in and cannot be changed. You cannot use information only known at runtime as a template parameter.
One way around this is if you known an upper-bound for the size of your bitset, and use that constant for your templated bitset structure. If the upper-bound is unacceptably large, you’ll have to use a different data structure, something akin to vector which is dynamically sized.