Re: Why is the return type of count_if() "signed" rather than "unsigned"?

Discussion in 'C++' started by Bo Persson, Jun 22, 2010.

  1. Bo Persson

    Bo Persson Guest

    xmllmx wrote:
    > On Jun 22, 4:04 pm, "Bo Persson" <> wrote:
    >> xmllmx wrote:
    >>> As we know, count_if() will never return a negative number. So, it
    >>> seems evident that the return type of count_if() should be
    >>> "unsigned integral type" rather than "signed integral type".

    >>
    >>> However, to my surprise, the C++ standard should define the return
    >>> type is "signed integer type", which causes a lot of conceptual
    >>> confusions and annoying compiling warnings such as
    >>> "signed/unsigned mismatch".

    >>
    >>> What's the rationale for the C++ standard committee to do so ?

    >>
    >>> Thanks in advance!

    >>
    >> The distance between two iterators is signed, because in the
    >> general case it could be negative. Here it can not, but count_if
    >> still uses the difference_type, to be consistent with other
    >> algorithms using iterators.
    >>
    >> Bo Persson

    >
    > Thank you for your quick response.
    >
    > Could you give me a convincing example to illustrate that it is
    > necessary to do so?


    You can look at std::distance instead, whihc computes the distance
    between two iterators. If you have random access iterators, like
    pointers or std::vector::iterator, this can result in a negative
    value. Therefore the difference_type is signed.


    >
    > Below is excerpted from the early MSDN. Hope this helps.
    >
    > ======================================
    > count_if
    > template<class InIt, class Pred, class Dist>
    > size_t count_if(InIt first, InIt last,
    > Pred pr);
    > The template function sets a count n to zero. It then executes ++n
    > for each N in the range [0, last - first) for which the predicate
    > pr(*(first + N)) is true. It evaluates the predicate exactly last -
    > first times.
    >
    > In this implementation, if a translator does not support partial
    > specialization of templates, the return type is size_t instead of
    > iterator_traits<InIt>::distance_type.


    This is a VERY old description (VC6 or worse?) for a compiler that
    could not do std::iterator_traits properly, and therefore has taken
    some short cuts in the standard library.

    The current release is VC10, which is from this millennium, and A LOT
    better at following the language standard.

    If you are at MSDN, please look here for a download of the free
    Express Edition:

    http://www.microsoft.com/express/Downloads/



    Bo Persson
    Bo Persson, Jun 22, 2010
    #1
    1. Advertising

  2. Bo Persson

    xmllmx Guest

    Re: Why is the return type of count_if() "signed" rather than"unsigned"?

    On Jun 22, 7:32 pm, "Bo Persson" <> wrote:
    > xmllmx wrote:
    > > On Jun 22, 4:04 pm, "Bo Persson" <> wrote:
    > >> xmllmx wrote:
    > >>> As we know, count_if() will never return a negative number. So, it
    > >>> seems evident that the return type of count_if() should be
    > >>> "unsigned integral type" rather than "signed integral type".

    >
    > >>> However, to my surprise, the C++ standard should define the return
    > >>> type is "signed integer type", which causes a lot of conceptual
    > >>> confusions and annoying compiling warnings such as
    > >>> "signed/unsigned mismatch".

    >
    > >>> What's the rationale for the C++ standard committee to do so ?

    >
    > >>> Thanks in advance!

    >
    > >> The distance between two iterators is signed, because in the
    > >> general case it could be negative. Here it can not, but count_if
    > >> still uses the difference_type, to be consistent with other
    > >> algorithms using iterators.

    >
    > >> Bo Persson

    >
    > > Thank you for your quick response.

    >
    > > Could you give me a convincing example to illustrate that it is
    > > necessary to do so?

    >
    > You can look at std::distance instead, whihc computes the distance
    > between two iterators. If you have random access iterators, like
    > pointers or std::vector::iterator, this can result in a negative
    > value. Therefore the difference_type is signed.
    >
    >
    >
    >
    >
    >
    >
    > > Below is excerpted from the early MSDN. Hope this helps.

    >
    > > ======================================
    > > count_if
    > > template<class InIt, class Pred, class Dist>
    > >     size_t count_if(InIt first, InIt last,
    > >         Pred pr);
    > > The template function sets a count n to zero. It then executes ++n
    > > for each N in the range [0, last - first) for which the predicate
    > > pr(*(first + N)) is true. It evaluates the predicate exactly last -
    > > first times.

    >
    > > In this implementation, if a translator does not support partial
    > > specialization of templates, the return type is size_t instead of
    > > iterator_traits<InIt>::distance_type.

    >
    > This is a VERY old description (VC6 or worse?) for a compiler that
    > could not do std::iterator_traits properly, and therefore has taken
    > some short cuts in the standard library.
    >
    > The current release is VC10, which is from this millennium, and A LOT
    > better at following the language standard.
    >
    > If you are at MSDN, please look here for a download of the free
    > Express Edition:
    >
    > http://www.microsoft.com/express/Downloads/
    >
    > Bo Persson- Hide quoted text -
    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -


    Thank you very much.

    I want to know WHY rather than WHAT. Much to my surprise, WHY does not
    the C++ standard define the return type as "unsigned integral type"?

    distance() may return negative number, it is imaginable.

    However, I cannot imagine count() returns a negative number. So, it is
    rather counterintuitive that its return type is "signed".
    xmllmx, Jun 22, 2010
    #2
    1. Advertising

  3. Bo Persson

    Bo Persson Guest

    xmllmx wrote:
    > On Jun 22, 7:32 pm, "Bo Persson" <> wrote:
    >> xmllmx wrote:
    >>> On Jun 22, 4:04 pm, "Bo Persson" <> wrote:
    >>>> xmllmx wrote:
    >>>>> As we know, count_if() will never return a negative number. So,
    >>>>> it seems evident that the return type of count_if() should be
    >>>>> "unsigned integral type" rather than "signed integral type".

    >>
    >>>>> However, to my surprise, the C++ standard should define the
    >>>>> return type is "signed integer type", which causes a lot of
    >>>>> conceptual confusions and annoying compiling warnings such as
    >>>>> "signed/unsigned mismatch".

    >>
    >>>>> What's the rationale for the C++ standard committee to do so ?

    >>
    >>>>> Thanks in advance!

    >>
    >>>> The distance between two iterators is signed, because in the
    >>>> general case it could be negative. Here it can not, but count_if
    >>>> still uses the difference_type, to be consistent with other
    >>>> algorithms using iterators.

    >>
    >>>> Bo Persson

    >>
    >>> Thank you for your quick response.

    >>
    >>> Could you give me a convincing example to illustrate that it is
    >>> necessary to do so?

    >>
    >> You can look at std::distance instead, whihc computes the distance
    >> between two iterators. If you have random access iterators, like
    >> pointers or std::vector::iterator, this can result in a negative
    >> value. Therefore the difference_type is signed.
    >>
    >>

    >
    > Thank you very much.
    >
    > I want to know WHY rather than WHAT. Much to my surprise, WHY does
    > not the C++ standard define the return type as "unsigned integral
    > type"?
    >
    > distance() may return negative number, it is imaginable.
    >
    > However, I cannot imagine count() returns a negative number. So, it
    > is rather counterintuitive that its return type is "signed".


    Ok. :)

    The reason is that some of the functions taking a pair of iterators
    can return signed values, so to be consistent all of them do. I
    believe it is that simple.


    Bo Persson
    Bo Persson, Jun 22, 2010
    #3
  4. Bo Persson

    Daniel Pitts Guest

    On 6/22/2010 6:30 AM, Bo Persson wrote:
    > xmllmx wrote:
    >> On Jun 22, 7:32 pm, "Bo Persson"<> wrote:
    >>> xmllmx wrote:
    >>>> On Jun 22, 4:04 pm, "Bo Persson"<> wrote:
    >>>>> xmllmx wrote:
    >>>>>> As we know, count_if() will never return a negative number. So,
    >>>>>> it seems evident that the return type of count_if() should be
    >>>>>> "unsigned integral type" rather than "signed integral type".
    >>>
    >>>>>> However, to my surprise, the C++ standard should define the
    >>>>>> return type is "signed integer type", which causes a lot of
    >>>>>> conceptual confusions and annoying compiling warnings such as
    >>>>>> "signed/unsigned mismatch".
    >>>
    >>>>>> What's the rationale for the C++ standard committee to do so ?
    >>>
    >>>>>> Thanks in advance!
    >>>
    >>>>> The distance between two iterators is signed, because in the
    >>>>> general case it could be negative. Here it can not, but count_if
    >>>>> still uses the difference_type, to be consistent with other
    >>>>> algorithms using iterators.
    >>>
    >>>>> Bo Persson
    >>>
    >>>> Thank you for your quick response.
    >>>
    >>>> Could you give me a convincing example to illustrate that it is
    >>>> necessary to do so?
    >>>
    >>> You can look at std::distance instead, whihc computes the distance
    >>> between two iterators. If you have random access iterators, like
    >>> pointers or std::vector::iterator, this can result in a negative
    >>> value. Therefore the difference_type is signed.
    >>>
    >>>

    >>
    >> Thank you very much.
    >>
    >> I want to know WHY rather than WHAT. Much to my surprise, WHY does
    >> not the C++ standard define the return type as "unsigned integral
    >> type"?
    >>
    >> distance() may return negative number, it is imaginable.
    >>
    >> However, I cannot imagine count() returns a negative number. So, it
    >> is rather counterintuitive that its return type is "signed".

    >
    > Ok. :)
    >
    > The reason is that some of the functions taking a pair of iterators
    > can return signed values, so to be consistent all of them do. I
    > believe it is that simple.

    That is not a good reason IMO.

    "Some guns can backfire, so to be consistent all of them do."

    Consistency is one consideration for API design, but should not be the
    overriding reason. Correctness should always come first. Ease of use
    is very important. Consistency is only important if it is sensible.
    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Jun 22, 2010
    #4
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,756
    Smokey Grindel
    Dec 2, 2006
  2. , India
    Replies:
    7
    Views:
    312
    kwikius
    Apr 28, 2008
  3. FE
    Replies:
    6
    Views:
    500
    Balog Pal
    Aug 4, 2009
  4. xmllmx
    Replies:
    2
    Views:
    375
    Saeed Amrollahi
    Jun 22, 2010
  5. xmllmx
    Replies:
    6
    Views:
    336
    gwowen
    Jun 23, 2010
Loading...

Share This Page