bitset assignment

A

Andy Skypeck

I have a question concerning the use of the [] operator in bitset
asignment.

bitset<4> a("1100");
bitset<4> b("0011");

a = a[j];

Is clearly supported by the standard, and works with all the C++
compilers I have worked with. But what about

a = b[j]; // ?

Assume that there are no indices out of range.
This works with gcc-3.* and MSVC 6 and higher but fails on an older
Borland compiler. I contend that this is ok because both a and
b[j] return type bitset<N>::reference. Others say no, but for
unspecified reasons. Does anybody
have an authoritative answer?
 
R

Ron Natalie

Andy Skypeck said:
Is clearly supported by the standard, and works with all the C++
compilers I have worked with. But what about

a = b[j]; // ?

Assume that there are no indices out of range.


It's supposed to work. That's the whole point of operator[] returning bitset::reference. Otherwise
it'd just return bool. Bitset::reference has a copy assignment operator.
 
D

Dan W.

I have a question concerning the use of the [] operator in bitset
asignment.

bitset<4> a("1100");
bitset<4> b("0011");

a = a[j];

Is clearly supported by the standard, and works with all the C++
compilers I have worked with. But what about

a = b[j]; // ?

Assume that there are no indices out of range.
This works with gcc-3.* and MSVC 6 and higher but fails on an older
Borland compiler. I contend that this is ok because both a and
b[j] return type bitset<N>::reference. Others say no, but for
unspecified reasons. Does anybody
have an authoritative answer?


Try casting b[1], say, to an int, and see what the different compilers
give you; assuming it's set, it should return 1. If it returns 2 it
probably means they took a shortcut that's confusing the assignment,
--i.e.: broken compiler.
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top