operator< for std::bitset

G

Guy.Tristram

Is there any good reason operator< is not defined for std::bitset? It
seems to me that:

1 - it would be useful.
2 - it is easy to implement inside the class template.
3 - it is impossible to implement efficiently (for bitsets too large
for to_ulong) outside of the class.

For now I will use boost::dynamic_bitset, which does implement it.

Guy
 
J

Juan Antonio Zaratiegui Vallecillo

(e-mail address removed) escribió:
Is there any good reason operator< is not defined for std::bitset? It
seems to me that:

1 - it would be useful.
2 - it is easy to implement inside the class template.
3 - it is impossible to implement efficiently (for bitsets too large
for to_ulong) outside of the class.

For now I will use boost::dynamic_bitset, which does implement it.

template <size_t Bits> bool operator<
(const std::bitset<Bits>& valueA,const std::bitset<Bits>& valueB)
{
return valueA.to_ulong()<valueB.to_ulong();
}

Of course, this does not work inside namespace std; but should work
right on most cases.

Best regards,

Zara
 
K

Kai-Uwe Bux

Victor said:
I would venture a guess that a bitset was created as a collection of
*independent* bits none of which is more important than the other, and
therefore the type does not have the semantics of comparison with 'less
than' operators, only for equality.


That's exactly on what the creators of the library do not agree with
you, I am guessing. You seem to attach some extra meaning to those
bits, the meaning the creators did not want to give the elements of the
collection.

Usefulness lies (as the disagreement shows) in the eye of the beholder. I
agree with the OP that the standard library didn't make a wise choice here:
there is no rationale _why_ bitset should only be used for cases where the
bits have no meanings. Put differently, which class should be used in cases
where the bits do have an additional meaning?


The efficiency concern of the OP (item 3) is perfectly valid. For that
reason alone, the standard library should at least specialize std::less<>
for bitset so that bitset object can be used in sets and maps with ease and
without performance penalty.


Best

Kai-Uwe Bux
 

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

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,830
Latest member
HeleneMull

Latest Threads

Top