Question

To define a bitset with 16 bits, it will be like:

std::bitset<16> bs(0x123);

If let me design a bitset class, I will probably make it like this:

mine::bitset bs(16, 0x123);

Is there any reason that std::bitset is implemented by template?

Is this a good pattern that we should apply in some situations?

Was it helpful?

Solution

Is there any reason that std::bitset is implemented by template? Is this a good pattern that we should apply in some situations?

Because std::bitset is designed to be a static bitset. In some cases, the compile-time size, will be very helpful, especially to the compiler, to optimize your program. If you want a dynamic bitset you can use boost::dynamic_bitset. Just like there is std::array and std::vector, there's always a place for statically sized containers and dynamically sized containers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top