-
std::array<bool, N> 은 비트를 다루도록 특수화 되어있지 않음프로그래밍/c++ 2020. 12. 1. 12:33반응형
std::vector<bool>은 내부적으로 bitset처럼 다뤄지도록 템플릿 특수화가 되어있다.
std::vector<bool> - cppreference.com
std::vector - cppreference.com
template class vector ; std::vector is a possibly space-efficient specialization of std::vector for the type bool. The manner in which std::vector is made space efficient (as well as whether it is optimized at all) is implementation defined. One potential
en.cppreference.com
그런데 std::array<bool, ...> 은 비트를 다루도록 템플릿 특수화가 되어있지 않다.
c++ - No class template specialization for array of bool? - Stack Overflow
No class template specialization for array of bool?
According to https://en.cppreference.com/, std::vector has a class template specialization, while std::array<bool, n=""> does not. Which are the reasons why it is not provided?</bool,>
stackoverflow.com
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 then 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).
개인적인 생각은 bool array의 비트 특수화가 필요하지 않았을 거 같은 것도 한 몫 한다고 본다. std::bitset이 이미 있으니 bit로 이용하고 싶다면 std::bitset을 이용하면 되니까.
std::bitset - cppreference.com
std::bitset - cppreference.com
The class template bitset represents a fixed-size sequence of N bits. Bitsets can be manipulated by standard logic operators and converted to and from strings and integers. bitset meets the requirements of CopyConstructible and CopyAssignable. [edit] Templ
en.cppreference.com
반응형'프로그래밍 > c++' 카테고리의 다른 글
Visual Studio, 데이터 변경 시 중단점 걸기. (0) 2022.04.26 Concepts (0) 2020.10.20 [C++] 정규 프로젝트 구조(Canonical Project Structure) (0) 2020.08.28 c++ namespace 로 감싸진 클래스, 구조체 전방 선언 (0) 2020.08.26 std::clamp (0) 2019.11.24