1

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;
}
Woody1193
  • 7,252
  • 5
  • 40
  • 90
  • 1
    You could make Foo into a template function and make size a non-type parameter. size has to be known at compile time otherwise this can't work. – Shafik Yaghmour Nov 13 '15 at 05:30
  • 1
    No, not with `std::bitset`. It requires a *compile-time* constant. You could use `vector`, or [`boost::dynamic_bitset`](http://www.boost.org/doc/libs/1_59_0/libs/dynamic_bitset/dynamic_bitset.html) or similar. – Igor Tandetnik Nov 13 '15 at 05:30
  • I thought so. I really like my `bitset`s because I can perform bitwise logical operations and single-bit logical operations with them but if it can't be done, it can't be done – Woody1193 Nov 13 '15 at 05:39

1 Answers1

2

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.

Hawkings
  • 533
  • 3
  • 16
  • Yes, I do know the maximum size I need but the implementation I want still depends on the size so I'll go with the `boost::dynamic_bitset`. Thanks – Woody1193 Nov 13 '15 at 06:38