Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSVS 2015: vector<bool> has no 'data' member

I have the following code that compiles just fine:

void foo::bar(const vector<int> arg) {
    int* ptr = arg.data();
    // do something with ptr
}

I need to overload this function for vector<bool>

void foo::bar(const vector<bool> arg) {
    int* ptr = arg.data(); 
    // error C2039: 'data': is not a member ofstd::vector<bool,std::allocator<_Ty>>'
    // do something with ptr
}

What is the reason for vector<bool> not to have a data() member?

Here (en.cppreference.com) I did not find somethign specific for bool case of std::vector.

The code is compiled with MSVS 2015.

like image 863
Alex Avatar asked Feb 07 '26 06:02

Alex


1 Answers

The whole point of the vector<bool> specialisation is that unlike all other vectors, the data in vector<bool> does not need to be stored as an array of bool. It can be stored more efficiently, by packing multiple bits in a single byte. Because of that, there is no bool * that could possibly be returned by a data() member.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!