operator< for std::bitset

Discussion in 'C++' started by Guy.Tristram@gmail.com, Nov 6, 2008.

  1. Guest

    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, 2008
    #1
    1. Advertising

  2. 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
     
    Juan Antonio Zaratiegui Vallecillo, Nov 6, 2008
    #2
    1. Advertising

  3. Kai-Uwe Bux Guest

    Victor Bazarov wrote:

    > 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
     
    Kai-Uwe Bux, Nov 6, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dill Hole
    Replies:
    2
    Views:
    455
    Mike Wahler
    Jul 5, 2003
  2. Geoffrey S. Knauth
    Replies:
    6
    Views:
    1,050
    Earl Purple
    Jan 18, 2006
  3. Martin T.
    Replies:
    7
    Views:
    840
    Martin T.
    Mar 10, 2008
  4. curiousEngine
    Replies:
    1
    Views:
    1,395
    James Kanze
    May 9, 2008
  5. Ninds
    Replies:
    14
    Views:
    866
    W Karas
    Dec 3, 2012
Loading...

Share This Page