Search code examples
c++templatesvariadic-templatesfold

Unary bitwise-or fold in bitset constructor


I'm trying to make a struct that encapsulates some information about functions. Included in it is a bitset representing certain true/false behaviors. I'm trying to fold a parameter pack into the constructor of the bitset, but it fails.

Here is my code:

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() {}
};

I expect the parsing of categories(categories_in|...) to understand that I'm trying to use a fold operation, but I get the error ‘categories_in’ is not a type and then expected ',' or '...' before '|' token.

Trying the line categories(...|categories_in) yields different but similar messages of the form "was expecting X instead of Y".

Using static cast to unsigned long long int (for the constructor parameter type) results in expected identifier before static cast, which feels weird since there's a name right before.

Any help getting the struct to work would be appreciated.


Solution

  • You need extra parenthesis for fold expression:

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

    And as you are in class definition, use {} (or = bitset<2>((categories_in|...))) instead of () to construct members.