Reading from an istream to a bool vector

Discussion in 'C++' started by Nomak, Aug 7, 2004.

  1. Nomak

    Nomak Guest

    Hello,

    With this code:

    $ cat -n ifs.cc
    1 #include <vector>
    2 #include <iostream>
    3
    4 using std::vector;
    5 using std::cin;
    6
    7 int
    8 main()
    9 {
    10 vector< bool > vb(1);
    11 vector< int > vi(1);
    12 bool b;
    13 int i;
    14
    15 cin >> b;
    16 cin >> i;
    17 cin >> (vb[0]);
    18 cin >> (vi[0]);
    19 return 0;
    20 }

    I have the following errors:

    $ g++-2.95.3 -Wall ifs.cc
    ifs.cc: In function `int main()':
    ifs.cc:17: initialization of non-const reference type `bool &'
    ifs.cc:17: from rvalue of type `bool'
    /.../iostream.h:217: in passing argument 1 of `istream::eek:perator
    >>(bool &)'


    $ g++-3.3.2 -Wall ifs.cc
    ifs.cc: In function `int main()':
    ifs.cc:17: error: no match for 'operator>>' in 'std::cin >>
    std::vector<bool, _Alloc>::eek:perator[](unsigned int) [with _Alloc =
    std::allocator<bool>](0)'

    $ g++-3.4.0 -Wall ifs.cc
    ifs.cc: In function `int main()':
    ifs.cc:17: error: no match for 'operator>>' in 'std::cin >>
    (&vb)->std::vector<bool, _Alloc>::eek:perator[] [with _Alloc =
    std::allocator<bool>](0u)'

    $ g++-3.4.1 -Wall ifs.cc
    ifs.cc: In function `int main()':
    ifs.cc:17: error: no match for 'operator>>' in 'std::cin >>
    (&vb)->std::vector<bool, _Alloc>::eek:perator[] [with _Alloc =
    std::allocator<bool>](0u)'

    So i guess this is the good behavior, but does anybody have a
    rationale for that? Why can i read into an int vector but not a bool
    one??

    --
    Nomak
    Nomak, Aug 7, 2004
    #1
    1. Advertising

  2. Nomak

    Ali Cehreli Guest

    On Sat, 07 Aug 2004 12:37:30 -0700, Nomak wrote:

    > $ cat -n ifs.cc
    > 1 #include <vector>
    > 2 #include <iostream>
    > 3
    > 4 using std::vector;
    > 5 using std::cin;
    > 6
    > 7 int
    > 8 main()
    > 9 {
    > 10 vector< bool > vb(1);
    > 11 vector< int > vi(1);
    > 12 bool b;
    > 13 int i;
    > 14
    > 15 cin >> b;


    b is an lvalue, we can pass it as a non-const reference to
    operator>>.

    > 16 cin >> i;
    > 17 cin >> (vb[0]);


    vb[0] is an rvalue because vector<bool>::eek:perator[] returns 'bool'
    (the value of the first object, by-value).

    > 18 cin >> (vi[0]);


    vi[0] is an lvalue because vector<int>::eek:perator[] returns 'int&'
    (the first object by-reference).

    > 19 return 0;
    > 20 }
    >
    > I have the following errors:
    >
    > $ g++-2.95.3 -Wall ifs.cc
    > ifs.cc: In function `int main()':
    > ifs.cc:17: initialization of non-const reference type `bool &'
    > ifs.cc:17: from rvalue of type `bool' /.../iostream.h:217: in passing
    > argument 1 of `istream::eek:perator
    >>>(bool &)'


    [...]

    > So i guess this is the good behavior, but does anybody have a rationale
    > for that? Why can i read into an int vector but not a bool one??


    There is a specialization for vector<bool> where the return type
    of operator[] differs from the return value of operator[] of the
    general vector implementation.

    vector<bool> is special in that it uses single bits to store the
    bool objects. In a typical implementation, an unsigned int may be
    used as the underlying data type. On a 32-bit machine this might
    have the first 32 objects of the container share one unsigned
    int.

    Since it is not possible to return a reference to a single bit,
    operator[] has to return by-value in the vector<bool>
    specialization.

    Ali
    Ali Cehreli, Aug 7, 2004
    #2
    1. Advertising

  3. Nomak

    Nomak Guest

    Ali Cehreli wrote:

    >> [...]
    >>
    >> So i guess this is the good behavior, but does anybody have a
    >> rationale for that? Why can i read into an int vector but not a
    >> bool one??

    >
    > There is a specialization for vector<bool> where the return type
    > of operator[] differs from the return value of operator[] of the
    > general vector implementation.
    >
    > vector<bool> is special in that it uses single bits to store the
    > bool objects. In a typical implementation, an unsigned int may be
    > used as the underlying data type. On a 32-bit machine this might
    > have the first 32 objects of the container share one unsigned
    > int.
    >
    > Since it is not possible to return a reference to a single bit,
    > operator[] has to return by-value in the vector<bool>
    > specialization.


    Thanks. It makes my loading code ugly...

    --
    Nomak
    Nomak, Aug 7, 2004
    #3
  4. Nomak

    Sylvain Audi Guest

    >> 16 cin >> i;
    >> 17 cin >> (vb[0]);

    >
    > vb[0] is an rvalue because vector<bool>::eek:perator[] returns 'bool'
    > (the value of the first object, by-value).


    [...]

    > Since it is not possible to return a reference to a single bit,
    > operator[] has to return by-value in the vector<bool>
    > specialization.


    That might not be that simple, otherwise it would be impossible to
    change any value in that bool array : if vb is rvalue, we can't
    assign anything to it.

    If I'm not mistaken, the vector<>::eek:perator[] method returns an object
    of type named "reference", which (in the case of bool vector) is
    probably some kind of proxy object for the targetted array element.

    Not too sure though, but I can't think of anything else that would do
    the job...

    Sylvain.
    Sylvain Audi, Aug 14, 2004
    #4
  5. Nomak

    Ali Cehreli Guest

    On Fri, 13 Aug 2004 17:47:11 -0700, Sylvain Audi wrote:

    >>> 16 cin >> i;
    >>> 17 cin >> (vb[0]);

    >>
    >> vb[0] is an rvalue because vector<bool>::eek:perator[] returns 'bool' (the
    >> value of the first object, by-value).

    >
    > [...]
    >
    >> Since it is not possible to return a reference to a single bit,
    >> operator[] has to return by-value in the vector<bool> specialization.

    >
    > That might not be that simple, otherwise it would be impossible to
    > change any value in that bool array : if vb is rvalue, we can't
    > assign anything to it.


    Thank you for the correction.

    > If I'm not mistaken, the vector<>::eek:perator[] method returns an object
    > of type named "reference", which (in the case of bool vector) is
    > probably some kind of proxy object for the targetted array element.


    Even when writing the above I was thinking about the posibility of such
    a proxy but didn't pay much attention to it. A quick test of mine made
    me believe that the operator>> for the proxy can do what the OP
    needed. I am not sure why it's not implemented that way.

    In any case, googling for older posts show that vector<bool> does not
    satisfy some of the container requirements anyway.

    Ali
    Ali Cehreli, Aug 16, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur

    vector<int> and vector<bool>

    Alex Vinokur, Jan 15, 2005, in forum: C++
    Replies:
    1
    Views:
    332
    Sharad Kala
    Jan 15, 2005
  2. Replies:
    3
    Views:
    904
  3. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  4. xmllmx
    Replies:
    5
    Views:
    589
    Jorgen Grahn
    Jun 15, 2010
  5. Abhishek Padmanabh
    Replies:
    1
    Views:
    372
    Bo Persson
    Mar 21, 2011
Loading...

Share This Page