Search code examples
c++vectorbooleanbackwards-compatibility

If std::vector<bool> was rewritten to use the standard vector implementation, how would that break old software?


According to the answers in this question, std::vector<bool> implements "special" logic (to allow each boolean value to be stored in a single bit, rather than taking up an entire byte), and because of that, it doesn't quite fulfil the requirements of an STL container and its use is therefore discouraged. However, the "special" logic is retained for backward-compatibility reasons.

My question is, if the C++ implementers were to throw out the "special" logic and turn std::vector<bool> into just another specialization of the std::vector template, what backwards compatibility problems would that cause? i.e. is there some special behavior that old software might be relying on that requires the bit-packing implementation to be retained? (the only thing I can think of is that some old software in a RAM-constrained environment might be relying on the eightfold-reduction in memory usage in order to function, but that seems like a relatively minor concern in most contexts)


Solution

  • Firstly, that would be an ABI break. A major reason why changing anything from the standard library is difficult.

    Secondly, anything using flip would break:

    #include <vector>
    
    int main() {
      std::vector<bool> vec { true };
      vec[0].flip(); // can't be done with regular bool
    }
    

    Thirdly, there would probably be other problems due to overload resolution someone probably relies on.


    Side note: You can always use boost::container::vector instead, which is not specialized for bool.