我试图执行两种不同的情况:为什么常变量不被视为编译时间常数有时
方案1:
const auto arraySize = 10; // fine, arraySize is constant
std::array<int, arraySize> data;
这里,ARRAYSIZE被视为编译时间常数,因此它被允许在std :: array中。
方案2:
int sz=10;
const auto arraySize = sz; // fine .
std::array<int, arraySize> data; //error , arraySize is not compile time constant .
在方案2中,ARRAYSIZE不被视为编译时尽管事实上,arrySize是SZ的恒定不变的副本。
为什么这两种方案进行了区别对待?
'arraySize'是一个* not *常量的常量副本...? – DevSolar
因为'const'并不意味着“编译时间常数”。在某些情况下,它可以是编译时,但不能保证,因此你不能在需要编译时常量的上下文中使用它们。改用'constexpr'! –