Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Why is the return type of count_if() "signed" rather than "unsigned"?

Reply
Thread Tools

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

 
 
Bo Persson
Guest
Posts: n/a
 
      06-22-2010
xmllmx wrote:
> On Jun 22, 4:04 pm, "Bo Persson" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
xmllmx
Guest
Posts: n/a
 
      06-22-2010
On Jun 22, 7:32*pm, "Bo Persson" <(E-Mail Removed)> wrote:
> xmllmx wrote:
> > On Jun 22, 4:04 pm, "Bo Persson" <(E-Mail Removed)> 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".
 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      06-22-2010
xmllmx wrote:
> On Jun 22, 7:32 pm, "Bo Persson" <(E-Mail Removed)> wrote:
>> xmllmx wrote:
>>> On Jun 22, 4:04 pm, "Bo Persson" <(E-Mail Removed)> 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


 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      06-22-2010
On 6/22/2010 6:30 AM, Bo Persson wrote:
> xmllmx wrote:
>> On Jun 22, 7:32 pm, "Bo Persson"<(E-Mail Removed)> wrote:
>>> xmllmx wrote:
>>>> On Jun 22, 4:04 pm, "Bo Persson"<(E-Mail Removed)> 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/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Why is the return type of count_if() "signed" rather than"unsigned"? xmllmx C++ 6 06-23-2010 01:18 PM
Why is the return type of count_if() "signed" rather than "unsigned"? xmllmx C++ 2 06-22-2010 10:21 AM
enum promote to bool type rather than Integer type? FE C++ 6 08-04-2009 03:21 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
How to return a stream of XML rather than a dataset JJA ASP .Net 0 12-01-2006 05:08 PM



Advertisments