Take the 2-minute tour ×

I have the following code which basically takes a vector and writes it to a file, and then opens the file and writes the content into a different vector.

#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

int main()
    vector<bool> q, p;
//         ^^^^
    q.resize(5, 0);
    q[0] = 1;
    q[2] = 1;
    q[4] = 1;

    ofstream ofile("file.log");

        ofile <<q[i]<<" ";


    ifstream ifile("file.log");


    int i = 0;
//        vvvvvvvvvvvv
    while(ifile>> p[i])
        cout <<i<<"\t"<<p[i]<<endl;


    return 0;

What I noticed is that this code compiles and runs with no problem when the vector contains double, int, and long data types, but produces an error if it is changed to bool. This is the error message that I get:

../src/timeexample.cpp:31: error: no match for ‘operator>>’ in ‘ifile >> p.std::vector<bool, _Alloc>::operator[] [with _Alloc = std::allocator<bool>](((long unsigned int)i))’

So, does anyone know why this happens?

Thank you

share|improve this question
There's no vector<bool> in the code you posted. –  user529758 May 9 '13 at 19:37
I believe a bool variable has a size of 1 bit. so a bitshift would do nothing. Check @Chris I was wrong read the specification as bit when it clearly says byte. –  Nomad101 May 9 '13 at 19:37
@Nomad101, Variables have to be at least one byte. –  chris May 9 '13 at 19:37
@chris you are totally right. I read the specification wrong thanks for setting me straight. –  Nomad101 May 9 '13 at 19:39
@Nomad101: bools are one byte, since the spec says nothing can be smaller than that (pedantically, that nothing smaller can be addressed). Infamously taking advantage of that is vector<bool>, which does use one bit for each bool, allocated in byte-size chunks. It's a Very Bad Thing, by virtue of being surprising behavior in a std container, and occasionally causes frustration. –  ssube May 9 '13 at 19:43
show 6 more comments

2 Answers

up vote 6 down vote accepted

std::vector<bool> is specialized to have space efficiency. operator[] wouldn't be able to return an addressable variable, so it returns a std::vector<bool>::reference proxy object instead. Just input it to a temporary variable and transfer it:

bool b;
while (ifile >> b) {
    p[i] = b;
    cout <<i<<"\t"<<b<<endl;
share|improve this answer
Thank you Chris. This fixes the problem. :) –  ipluto May 9 '13 at 19:55
add comment

vector<bool> is not what you think. It is a specialization of std::vector that is optimized for space and not for operations on elements, trying to reduce the bool storage by using bits to represent bool values. When bits are used the stream operators cannot work anymore.

Se here for the important results of the optimization:

  • The storage is not necessarily an array of bool values, but the library implementation may optimize storage so that each value is stored in a single bit.
  • Elements are not constructed using the allocator object, but their value is directly set on the proper bit in the internal storage.
  • Member function flip and a new signature for member swap.
  • A special member type, reference, a class that accesses individual bits in the container's internal storage with an interface that emulates a bool reference. Conversely, member type const_reference is a plain bool.
  • The pointer and iterator types used by the container are not necessarily neither pointers nor conforming iterators, although they shall simulate most of their expected behavior.

There is another data structure that you can use int this way, if you know how many bool values you have at compile time : bitset

It has stream operators defined, which is what you are aiming at.

share|improve this answer
Thanks for the detailed explanation. It definitely had something for me to learn. –  ipluto May 9 '13 at 19:56
@ipluto no problem :) –  tmaric May 9 '13 at 19:58
add comment

Your Answer


