使用 const 数组元素初始化
Initializing with const array element
为什么编译器 (VC++) 不允许 (错误 C2975)
const int HASH[] = {24593, 49157};
bitset<HASH[0]> k;
我能做些什么来克服这个问题(使用数组中的常量值初始化模板)?
本地 const
对象不符合常量表达式的条件,但std::bitset<N>
需要非类型模板参数N
为常量表达式。具有初始值设定项的const
整型对象确实有资格作为常量表达式。在所有其他情况下,您将需要constexpr
(我不知道MSVC++是否支持constexpr
)。例如:
#include <bitset>
struct S { static int const member = 17; };
int const global_object = 17;
int const global_array[] = { 17, 19 };
int constexpr global_constexpr_array[] = { 17, 19 };
int main()
{
int const local = 17;
int const array[] = { 17, 19 };
int constexpr constexpr_array[] = { 17, 19 };
std::bitset<S::member> b_member; // OK
std::bitset<global_object> b_global; // OK
std::bitset<global_array[0]> b_global_array; // ERROR
std::bitset<global_constexpr_array[0]> b_global_constexpr_array; // OK
std::bitset<local> b_local; // OK
std::bitset<array[0]> b_array; // ERROR
std::bitset<constexpr_array[0]> b_constexpr_array; // OK
}
综上所述,您确定是否真的要对数组指定的元素数进行std::bitset<N>
?如果你真的对值的位感兴趣,你宁愿使用这样的东西:
std::bitset<std::numeric_limits<unsigned int>::digits> k(HASH[0]);
相关文章:
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 在函数内部的声明中初始化数组,并在外部使用它
- 为什么用结构初始化数组需要指定结构名称
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- C++使用另一个数组和新值初始化数组
- 初始化数组、"memset"或" {//value} "的最佳方法是什么?
- 在 constexpr 构造函数中初始化数组是否合法?
- 我可以初始化 const 实例,以便我可以将其用作 const 来初始化数组吗?
- 在构造函数中初始化数组
- 是否可以使用函数返回的值初始化数组
- 使用宏使用额外元素初始化数组
- 在循环中显示不同值的初始化数组
- 如何在macOS中的旧扩展clang和gcc编译器中初始化数组和向量
- 使用可变模板列表初始化数组,并放置new
- 使用函数从 Visual Studio 2017 中的 main 创建和初始化数组
- 使用 c++ 中的函数初始化数组
- 如何在类中初始化数组并为第一个元素设置值
- C 使用每个元素参考初始化数组
- 当初始化列表中没有其他元素时,初始化数组的其余元素
- 如何使用初始化列表初始化数组元素