So I wrote this code ->
#include <iostream>
#include <bitset>
int main(){
int num, temp, digits = 0;
std::cin >> num;
temp = num;
while(temp){
temp /= 10;
++digits;
}
const int size = digits;
std::bitset<size> a(num);
std::cout << a << std::endl;
return 0;
}
The bitset container isnt accepting the const integer size as a parameter and throwing an error -Non-type template argument is not a constant expression
. I want to know why this is happening as size has been declared as a constant and it's value isnt gonna change during the run-time of my program ?
A const
variable can be interpreted differently, depending on what is assigned to it.
When assigned a compile time constant: it will be a compile time constant. That means that during compilation the constant value can be directly used in-place.
When assigned from another variable (which is not a compile time constant) : the new variable is unmodifilable. In that sense the variable is not a compile time constant. It cannot be modified in that block of code.
A template requires a compile time constant.