Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vector<bool> special reference treatment?

2 views
Skip to first unread message

klaas

unread,
Aug 22, 2003, 11:32:54 AM8/22/03
to
the following code gives rise to the beneath error message, only when
a matrix object is instantiated as matrix<bool>, not with matrix<float>:

/*returns a reference to the object at position (Row,Col) in matrix*/
template <class num_type,template <class T> class functor>
num_type & matrix<num_type,functor >::operator()(const int Row,const
int Col)
{if (Row<rows && Col<cols)
{vector<num_type> & x=matrix_core[Row];
//num_type & y=x[Col];<-is also faulty ,
//return matrix_core[Row][Col]; <- same problem
return x[Col];
}
// else return (*num_type)0;
}
indeed, matrix_core is of type vector<vector<num_type> >

apemonkie.cpp:27: instantiated from here
matrix.cpp:301: could not convert `std::vector<bool,
_Alloc>::operator[](unsigned int) [with _Alloc =
std::allocator<bool>](Col)'
to `bool&'
make: *** [apemonkie.o] Error 1

I really do need a reference since I need to change values inside the
matrix.
STL-manual says that for a vector<T> foo;
the expression foo[bar]; should return a reference to an object of type T.
especially when you read the following, taken out of the bit_vector manual:
>reference in "reference operator[](size_type n)"
> A proxy class that acts as a reference to a single bit;
>the reason it exists is to allow expressions like V[0] = true.
>(A proxy class like this is necessary, because the C++ memory
>model does not include independent addressing of objects smaller
>than one byte.) The public member functions of reference are operator
>bool() const, reference& operator=(bool), and void flip(). That is,
>reference acts like an ordinary reference: you can convert a reference
>to bool, assign a bool value through a reference, or flip the bit that
>a reference refers to.


So it seems that there is special behavior for vector<bool> which sounds
reasonable (bit_vector is the same thing right), but how can I get the
references?

thanks in advance,

klaas

Howard Hinnant

unread,
Aug 22, 2003, 11:44:19 AM8/22/03
to
In article <QIq1b.19906$tK5.2673473@zonnet-reader-1>,
klaas <j...@zonnet.ru> wrote:

> So it seems that there is special behavior for vector<bool> which sounds
> reasonable (bit_vector is the same thing right), but how can I get the
> references?

Right, vector<bool> is special. It is "bit packed".

You can do this:

template <class num_type,template <class T> class functor>

typename std::vector<num_type>::reference


matrix<num_type,functor >::operator()(const int Row,const int Col)
{

...
typename std::vector<num_type>::reference y = x[Col];
...
}

For every num_type but bool, reference will be num_type&. For bool it
will be a class that mostly acts like a real reference.

-Howard

Rob Williscroft

unread,
Aug 22, 2003, 11:45:55 AM8/22/03
to
klaas wrote in news:QIq1b.19906$tK5.2673473@zonnet-reader-1:

> apemonkie.cpp:27: instantiated from here
> matrix.cpp:301: could not convert `std::vector<bool,
> _Alloc>::operator[](unsigned int) [with _Alloc =
> std::allocator<bool>](Col)'
> to `bool&'
> make: *** [apemonkie.o] Error 1
>

std::vector< bool > is a specialization, it actually packs
the bools into single bits and thus uses (size() / CHAR_BITS)
bytes of storage rather than size() bytes.

As a consiquence it's operator[] returns a proxy object since
its not possible to have a reference or pointer to a single bit.

Try changing your bool paramiter to char or write a class
that wraps up the functionality of a bool.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/

klaas

unread,
Aug 22, 2003, 12:13:00 PM8/22/03
to
Rob Williscroft wrote:

> klaas wrote in news:QIq1b.19906$tK5.2673473@zonnet-reader-1:
>
>
>>apemonkie.cpp:27: instantiated from here
>>matrix.cpp:301: could not convert `std::vector<bool,
>> _Alloc>::operator[](unsigned int) [with _Alloc =
>>std::allocator<bool>](Col)'
>> to `bool&'
>>make: *** [apemonkie.o] Error 1
>>
>
>
> std::vector< bool > is a specialization, it actually packs
> the bools into single bits and thus uses (size() / CHAR_BITS)
> bytes of storage rather than size() bytes.

> Rob.
I know that, but when you look at the manual:
http://www.sgi.com/tech/stl/bit_vector.html


>reference in "reference operator[](size_type n)"
> A proxy class that acts as a reference to a single bit;
>the reason it exists is to allow expressions like V[0] = true.
>(A proxy class like this is necessary, because the C++ memory
>model does not include independent addressing of objects smaller
>than one byte.) The public member functions of reference are operator
>bool() const, reference& operator=(bool), and void flip().

and ESPECIALLY here:


That is,
>reference acts like an ordinary reference: you can convert a reference
>to bool, assign a bool value through a reference, or flip the bit that
>a reference refers to.

the code should work right?
or would I have to change the return value to bool instead of bool &?
That would seem odd though, because then the assignment trough the
reference would not be totally valid?

klaas...

Rob Williscroft

unread,
Aug 22, 2003, 12:35:46 PM8/22/03
to
klaas wrote in news:pir1b.19909$tK5.2676971@zonnet-reader-1:

Then you should try:

... matrix< T >::whatever_is_at_line_301()
{
typedef typename std::vector< T >::reference result_type;

result_type r = this->m_vectorT[ whatever ];

// ...
}


Rob.
--
http://www.victim-prime.dsl.pipex.com/

klaas

unread,
Aug 22, 2003, 1:25:15 PM8/22/03
to

>>and ESPECIALLY here:
>>That is,
>>
>>>reference acts like an ordinary reference: you can convert a
>>>reference to bool, assign a bool value through a reference, or flip
>>>the bit that a reference refers to.
>>
>>the code should work right?
>>or would I have to change the return value to bool instead of bool &?
>>That would seem odd though, because then the assignment trough the
>>reference would not be totally valid?
>
>
> Then you should try:
>
> ... matrix< T >::whatever_is_at_line_301()
> {
> typedef typename std::vector< T >::reference result_type;
>
> result_type r = this->m_vectorT[ whatever ];
>
> // ...
> }
>
>
> Rob.
No, that's just delaying finding a solution. I think it can't be
solved:(. The manual says that the proxy just offers an interfave to
assign a bool to the object refered to and that you can assign to a
bool. Nothing has been promised about adding a level of indirection to
that service, which is what returning a reference to a bool would amount
to.

I'd have to add a separate way of handeling matrices of booleans and
that is not within the scope of my project. I'll use integers instead.

thanks

klaas

0 new messages