473,657 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,templa te <class T> class functor>
num_type & matrix<num_type ,functor >::operator()(c onst int Row,const
int Col)
{if (Row<rows && Col<cols)
{vector<num_typ e> & 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<n um_type> >

apemonkie.cpp:2 7: instantiated from here
matrix.cpp:301: could not convert `std::vector<bo ol,
_Alloc>::operat or[](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 4137
In article <QIq1b.19906$tK 5.2673473@zonne t-reader-1>,
klaas <ja*@zonnet.r u> 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,templa te <class T> class functor>
typename std::vector<num _type>::referen ce
matrix<num_type ,functor >::operator()(c onst int Row,const int Col)
{
...
typename std::vector<num _type>::referen ce 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.1990 6$tK5.2673473@z onnet-reader-1:
apemonkie.cpp:2 7: instantiated from here
matrix.cpp:301: could not convert `std::vector<bo ol,
_Alloc>::operat or[](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
14361
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' Effective STL's Item 18: Avoid using vector<bool>. Plus, I'm loath to use bit_vector since SGI's STL implementation says it will soon be dropped on the floor (http://www.sgi.com/tech/stl/bit_vector.html).
3
3455
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
1555
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 array from the data stored in a std vector. I load loads of floats into a vector at the moment to store vertex infomration for openGL, but to render them, I need to pass a pointer to the beginning of the array to OpenGL, not a vector. At the
1
2405
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
3540
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 vector::<bool>::iterator (or reference). However, given a std::vector<bool> a, all a.begin(), a etc are instances of a proxy class so I can not do things like &*a.begin(). Is there a safe way to get the information I need? Many thanks in advance.
12
6715
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 (myboolarray) { // do this... } if not, is there another solution?
2
4757
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, _M_mask = 8} (gdb) p layout._visible & 0x80 Structure has no component named operator&. (gdb)
6
6806
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 vector<bool>, does each element takes 4 bytes instead of 1 bit? I am using gcc3.4.4. There is a bit_vector which is kind of old so I wont use that. Any other choices? Thanks ahead. zl2k
8
5359
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 possible to "spoof" std::vector into implementing a "true" vector of bool rather than the specialisation? Say I do: typedef bool boolreally; std::vector<booleallybvec;
0
8425
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.