Shu*_*and 3 c++ constants bitset std-bitset
所以我写了这段代码->
#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;
}
Run Code Online (Sandbox Code Playgroud)
位集容器不接受 const 整数大小作为参数并抛出错误 - Non-type template argument is not a constant expression。我想知道为什么会发生这种情况,因为大小已被声明为常量,并且它的值在程序运行时不会改变?
变量const可以有不同的解释,具体取决于分配给它的内容。
当分配编译时常量时:它将是编译时常量。这意味着在编译期间可以直接就地使用常量值。
当从另一个变量(不是编译时常量)分配时:新变量是不可修改的。从这个意义上说,该变量不是编译时常量。无法在该代码块中对其进行修改。
模板需要一个编译时间常数。
| 归档时间: |
|
| 查看次数: |
867 次 |
| 最近记录: |