简体   繁体   中英

std:bitset c++ external initialization

I want to wrap or cast a std:bitset over a given constant data arrary or to formulate it differently, initialize a bitset with foreign data.

The user knows the index of the bit which he can check then via bitset.test(i) . Data is big, so it must be efficient. (Machine bitorder does not matter, we can store it in the right way). Thats what I tried:

constexpr uint32_t          data[32] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};
constexpr std::bitset<1000>  bset(1); //bit bitset initialized with a value here
constexpr  std::bitset<1000> bset2(data); //init it with our data, this is not working

The number of bits is 32*32=1024 that is held by data. With my bitset i can address the almost full range. User does not need more than 1000. Can someone please explain to me how this is done in cpp in my example above with bset2?

Unfortunately std::bitset does not have suitable design for what you want.

It is not designed an aggregate (like std::array is) so aggregate initialiation is impossible (and also copying bits into it with std::memcpy is undefined behavior).

It can take only one unsigned long long in constexpr constructor.

The operator [] and set method will become constexpr in C++23 so there will be a way after that.

Just use constexpr raw array or std::array and add bit accessing methods until then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM