gpt4 book ai didi

c++ - 一元按位或折叠位集构造函数

转载 作者:行者123 更新时间:2023-11-30 03:22:30 25 4
gpt4 key购买 nike

我正在尝试创建一个结构来封装有关函数的一些信息。其中包括一个表示某些真/假行为的位集。我试图将参数包折叠到位集的构造函数中,但它失败了。

这是我的代码:

template<uint8_t ID_in, uint8_t...categories_in>
struct Function_Data {

static constexpr const uint8_t ID = ID_in;

// only two categories so far
static constexpr const bitset<2> categories(categories_in|...);

constexpr inline explicit Function_Data() {}
};

我希望 categories(categories_in|...) 的解析能够理解我正在尝试使用折叠操作,但我收到错误消息 'categories_in' is not a在“|” 标记之前输入 和 expected ',' or '...'。



尝试行 categories(...|categories_in) 会产生不同但相似的消息,其形式为“was expecting X instead of Y”。



对 unsigned long long int(对于构造函数参数类型)使用静态转换会导致静态转换前的预期标识符,这感觉很奇怪,因为前面有一个名称。



如果您能帮助该结构正常工作,我们将不胜感激。






最佳答案





fold expression 需要额外的括号:



static constexpr const bitset<2> categories{(categories_in|...)};

在类定义中,使用 {} (或 = bitset<2>((categories_in|...)) )而不是 ()构造成员。

关于c++ - 一元按位或折叠位集构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51086666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com