Weird error.

A

Adam Benson

Hi,

Does anyone know why the top example is OK, but the bottom one gives a warning ?

void X()

{

void * p;



// This is OK ...

std::vector<int> int_v;

std::vector<int>::iterator int_i;

p = (void *) &( *(int_v.begin() + 5) );



// This gives : warning C4238: nonstandard extension used : class rvalue used as lvalue

std::vector<bool> bool_v;

std::vector<bool>::iterator bool_i;

p = (void *) &( *(bool_v.begin() + 5) );

}

Cheers,

Adam.

--
===========================
Adam Benson
Omnibus Systems,
Leics. UK
Email : (e-mail address removed)
 
T

Tamas Demjen

Adam said:
Does anyone know why the top example is OK, but the bottom one gives a
warning ?

Because vector<bool> is not an STL container. It should have been called
bitvector or something. I know this is very confusing, but this is what
we have to live with. As odd as it looks, you can't store bools in a
vector. Use deque<bool> instead.

Tom
 
G

Guest

Hi,

Does anyone know why the top example is OK, but the bottom one gives a warning ?

void X()

{

void * p;



// This is OK ...

std::vector<int> int_v;

std::vector<int>::iterator int_i;

p = (void *) &( *(int_v.begin() + 5) );



// This gives : warning C4238: nonstandard extension used : class rvalue used as lvalue

std::vector<bool> bool_v;

std::vector<bool>::iterator bool_i;

p = (void *) &( *(bool_v.begin() + 5) );

}

Cheers,

Adam.

--
===========================
Adam Benson
Omnibus Systems,
Leics. UK
Email : (e-mail address removed)


Hi Adam,
Please refer to this article
http://www.gotw.ca/publications/N1185.pdf

Basically, the implementation of std::vector<bool> is in the form of
bitfields. When you add 5 to the iterator and dereference the vector<bool>,
you are actually pointing to the 5th "bit" in the byte.
Then you take an address of that, which is not correct. In order to take the
address you must be at the byte boundary and not at the bit boundary.

So, in short try to avoid using vector<bool> as it is non conformant as you
will read from the article I mentioned.

Thanks,
Kapil Khosla
 
A

Adam Benson

Thanks for the responses.

I thought I was going nuts.

AB

===========================
Adam Benson
Omnibus Systems,
Leics. UK
Email : (e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top