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

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

  1. xmllmx

    xmllmx Guest

    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!
     
    xmllmx, Jun 22, 2010
    #1
    1. Advertising

  2. xmllmx

    Bo Persson Guest

    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
     
    Bo Persson, Jun 22, 2010
    #2
    1. Advertising

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

    Hi

    On Jun 22, 9:55 am, 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".
    >


    According to C++ standard document, the return type of count/count_if
    is: iterator_traits<InputIterator>::difference_type.
    Because it's like the distance of two pointers
    it's may be positive or negative. However count_if accepts Input
    Iterator
    so such difference is always positive.

    Frankly, I don't think the following code issues a lot of annoying
    warnings:

    #include <algorithm>
    #include <vector>
    #include <iostream>
    bool is_odd(int i) { return i % 2 != 0; }
    int main()
    {
    using namespace std;
    vector<int> v;
    for (int i = 0; i < 1000; ++i)
    v.push_back(i);

    int c = count_if(v.begin(), v.end(), is_odd);

    cout << c << '\n';
    return 0;
    }

    I compiled and ran using VS 2008 (VC++ 9).
    > What's the rationale for the C++ standard committee to do so ?
    >
    > Thanks in advance!


    Regards,
    -- Saeed Amrollahi
     
    Saeed Amrollahi, Jun 22, 2010
    #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. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,072
    Smokey Grindel
    Dec 2, 2006
  2. , India
    Replies:
    7
    Views:
    332
    kwikius
    Apr 28, 2008
  3. FE
    Replies:
    6
    Views:
    532
    Balog Pal
    Aug 4, 2009
  4. xmllmx
    Replies:
    6
    Views:
    358
    gwowen
    Jun 23, 2010
  5. Bo Persson
    Replies:
    3
    Views:
    392
    Daniel Pitts
    Jun 22, 2010
Loading...

Share This Page