AutosarC++18_10-A18.1.2ΒΆ

The std::vector<bool> specialization shall not be used

Required inputs: IR

Rationale

The std::vector<bool> specialization does not work with all STL algorithms as expected. In particular, operator[]() does not return a contiguous sequence of elements as it does for the primary template std::vector<T>.

The C++ Language Standard guarantees that distinct elements of an STL container can be safely modified concurrently, except when the container is a std::vector< bool>.

Note that std::bitset<N>, std::array<bool, N>, std::deque<bool>, or using std::vector with a value type which wraps bool are possible alternatives.

Example
// $Id: A18-1-2.cpp 312108 2018-03-16 17:56:49Z jan.babst $
    #include <cstdint>
    #include <vector>

    class BoolWrapper
    {
       public:
         BoolWrapper() = default;
         constexpr BoolWrapper(bool b) : b_(b) {}           // implicit by design
         constexpr operator bool() const { return b_; }     // implicit by design
       private:
         bool b_{};
    };

    void Fn() noexcept
    {
        std::vector<std::uint8_t> v1;                               // Compliant
        std::vector<bool> v2;                                       // Non-compliant
        std::vector<BoolWrapper> v3{true, false, true, false};      // Compliant
    }
See Also
  • HIC++ v4.0 [9]: 17.1.1: Do not use std::vector<bool>.

Possible Messages

Key

Text

Severity

Disabled

type_used

The type vector<bool> shall not be used.

None

False

Options