Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does std::count return a signed integer? [duplicate]

I was really surprised to see that std::count returned a iterator_traits<InputIterator>::difference_type, which in turns refers to a long int on my platform.

Why is that? A negative count elements within a container doesn’t make any sense.

like image 960
qdii Avatar asked Feb 11 '13 12:02

qdii


1 Answers

It's actually a std::ptrdiff_t, which has to be a signed integer. It has to be signed because it can be used as the difference between two iterators, and that can of course be negative.

like image 69
Some programmer dude Avatar answered Sep 21 '22 23:09

Some programmer dude