According to https://en.cppreference.com/, std::vector<bool>
has a class template specialization, while std::array<bool, N>
does not. Which are the reasons why it is not provided?
When std::vector
was introduced, a specialization for bool
was considered a good idea. Basically, at that time, the average computer had 4 MB of memory, so saving computer memory was quite important. Nowadays we just say "memory is cheap" (quote from Uncle Bob).
Later it turned out that this specialization creates more problems than it is worth.
The issue is that the address to one of the elements of such a vector is a complex object (it has to store information on which bit holds which value) compared to regular old-fashioned C-array bool a[]
.
Since compatibility must be retained, this specialization can't be dropped, but based on that lesson, the same approach was not applied to std::array
.
Another reason is that std::array
is supposed to be a C-array wrapper, so it must be as similar to bool a[N]
as possible, and must produce the same machine code when used.
And the last thing, as Cody Gray points out in a comment under question, std::bitset
is a constant size array of bits, so such functionality is already available (and can be used if needed).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With