1

I want to decide size of bitset during runtime.
But std::bitset<N> only accepts constexpr values for N, not even const values.
Which means size of bitset must be decided before compiling.

I know std::vector provides optimization for bool array,
but it lacks those useful bitset members I need.

Question 1: Why does N has to be constexpr value?
Well I'm guessing that's because bitset is template, but still, this is massive inconvenience.
Bitset could have been a class rather than a template.
It's constructor can take size_t as argument, than I can create variable length bitset.
The same question goes for std::array.
Could've been std::array<type> foo(size, values).

Question 2: Is there any 'Hacks' that lets me create variable-length bitset?
I'm pretty sure there won't be any, considering how template works.
But just maybe, there is some clever trick :)
If not, I'll have to use std::vector<bool> and implement bitset members myself.

NutCracker
  • 11,485
  • 4
  • 44
  • 68
WieeRd
  • 792
  • 1
  • 7
  • 17

2 Answers2

2

Why does N has to be constexpr value?

You are right. Size of both std::bitset and std::array is specified as a template parameter, so it cannot be set during runtime.

However, in the past there were some proposals for introducing dynamic arrays in the C++ standard. One of them was called std::dynarray. Eventually, it won't be introduced into standard but you can see here a more elaborative description of its lifetime.

Is there any 'Hacks' that lets me create variable-length bitset?

If you have access to the Boost library, you can use its dynamic_bitset.

NutCracker
  • 11,485
  • 4
  • 44
  • 68
-1

Bitset could have been a class rather than a template.

It is made for a purpose and if that is not yours, you simply have to use something different.

If you want to store single bits, which means you like to store bool with variable size, you simply can use std::vector< bool >

If not, I'll have to use std::vector and implement bitset members myself.

A bitset is a container of bits. So what do you mean by implementing bitset yourself?

Klaus
  • 24,205
  • 7
  • 58
  • 113
  • By implementing bitset myself, I meant bitset member functions like flip, set, reset, to_string, etc. std::bitset even supports init by string or integer value. std::vector doesn't, so I'll have to make them myself. Also, "It is made for a purpose". Well at least let me know what that purpose is than! Historical reason? – WieeRd Mar 01 '20 at 08:14
  • "Historical reason?" Can't catch your point? Do you feel templates are historical? Sorry, no idea what your intention is... – Klaus Mar 01 '20 at 08:28
  • Alright. To me, bitset would have been more useful if it was a class. But it isn't. So, why? Is there a logical or historic reason? Or maybe there is no reason and might change in the future? Anyways, looks like you don't know it either cuz your answer was more like "Well just deal with it". I will deal with it but hey, I was just curious :) – WieeRd Mar 01 '20 at 09:55
  • @Ultim8_Clock The general idea to use a template is to enable all possible optimizations which couldn't be done if all parameters are runtime. As this, here the purpose for the template is to have a fast as possible way to access single bits. If using something like a container class with runtime variable size is more comfortable but will definitely reduce speed. I am with you that having the same interface for compile time constant bitset also for a runtime variant would be nice. – Klaus Mar 02 '20 at 09:01