473,322 Members | 1,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

vector<bool> special reference treatment?

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

Jul 19 '05 #1
3 4113
In article <QIq1b.19906$tK5.2673473@zonnet-reader-1>,
klaas <ja*@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
Jul 19 '05 #2
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/
Jul 19 '05 #3
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

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Scott Brady Drummonds | last post by:
Hi, everyone, I have a program in which I need to store a series of Boolean values. A vector<bool> would be ideal. However, I'm concerned about this data structure because of Scott Meyers'...
3
by: Alexandros | last post by:
Hi. How can I create a vector<bool> efficiently from a char* or a vector<char> ? For example, if char* c == (8,10) I want vector<bool> v to be: (0000100000001010)
11
by: Michael | last post by:
Righty, 1: Is there a standard library that contain matrices and complex numbers. I need to find eigen values of a 3x3 matrix. 2: Is there a way of getting the pointer to the start of an...
1
by: Alex Vinokur | last post by:
------ foo.cpp ------ #include <vector> using namespace std; int main() { const vector<int> v1 (10); const vector<bool> v2 (10); &v1;
8
by: Bo Peng | last post by:
Dear list, I am using std::vector<bool> (bit_vector) to store my bit sequence. To access the same sequence from C (to expose to a python module), I need to know the pointer and offset of...
12
by: Piotr | last post by:
In effective STL, it said one should not use vector<bool> but use dequeue<bool> instead. But can dequeue<bool> has random access iterator? and I do this? dequeue<bool> myboolarray; if...
2
by: huomingxu | last post by:
in gdb, when print an element of vector<bool>, it returns the offset of the bi t instead of the value. e.g: (visible is of type vector<bool>) (gdb) p layout._visible $15 = {_M_p = 0x9817d00,...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
8
by: Lionel B | last post by:
On my platform I find that the std::vector<boolspecialisation incurs a significant performance hit in some circumstances (when compared, say, to std::vector<intprogrammed analagously). Is it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.