Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No class template specialization for array of bool?

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?

like image 406
nyarlathotep108 Avatar asked Nov 16 '20 16:11

nyarlathotep108


1 Answers

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).

like image 117
Marek R Avatar answered Oct 12 '22 22:10

Marek R