By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,945 Members | 1,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,945 IT Pros & Developers. It's quick & easy.

operator< for std::bitset

P: n/a
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
Nov 6 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Gu**********@gmail.com wrote:
Is there any good reason operator< is not defined for std::bitset?
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.
It
seems to me that:

1 - it would be useful.
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.
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.
That's what the third-party libraries are for, extending the standard
library, providing elements that do not exist in it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 6 '08 #2

P: n/a
Gu**********@gmail.com 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 Bitsbool 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
Nov 6 '08 #3

P: n/a
Victor Bazarov wrote:
Gu**********@gmail.com wrote:
>Is there any good reason operator< is not defined for std::bitset?

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.
It
seems to me that:

1 - it would be useful.

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?

>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.
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
Nov 6 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.