Search code examples
c++bitsetconstant-expression

Constant Expression Parameters


I have a function that creates a std::bitset, whose length is a constant function parameter. It doesn't work because the constant isn't an "integral constant expression". Is there any way I can make this work?

For reference:

void Foo(const std::string &data, const unsigned int size) {
    std::bitset<size> Bar(data);
    // Do something
    return;
}

Solution

  • The size of a std::bitset needs to be evaulable at compile time. You can either make it big enough (in case you know the maximum size that you may need) or use another alternative, like boost::dynamic_bitset as Igor Tandetnik suggested.