operator< for std::bitset

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guy.Tristram@gmail.com

    operator< for std::bitset

    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
  • Victor Bazarov

    #2
    Re: operator&lt; for std::bitset

    Guy.Tristram@gm ail.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

    Comment

    • Juan Antonio Zaratiegui Vallecillo

      #3
      Re: operator&lt; for std::bitset

      Guy.Tristram@gm ail.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<Bit s>& valueA,const std::bitset<Bit s>& valueB)
      {
      return valueA.to_ulong ()<valueB.to_ul ong();
      }

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

      Best regards,

      Zara

      Comment

      • Kai-Uwe Bux

        #4
        Re: operator&lt; for std::bitset

        Victor Bazarov wrote:
        Guy.Tristram@gm ail.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

        Comment

        Working...