Just realized that std::count_if
returns a signed value.
Why is it designed this way? It makes no sense (the result can only be a natural number, i. e. non-negative integer) in my opinion, as it doesn't allow doing something as simple as comparing this result to the container's size()
without either getting a warning or using explicit type conversion.
I really think the return type should have size_type
.
Am I missing something?
I think the return type aims to be compatible to std::count
which takes two iterators (think of pointers) and return the values in between (which you can think of as a difference of two pointers). A pointer difference (as used in ptrdiff_t
) has to be a signed value.
Thanks to the compatibility to std::count
you can easily compare the results of these two functions.
Edit: There is no range related drawback using a signed value here, since the value will at least be in the range [0, std::count]
which itself will be in the range [0, end_ptr - start_ptr]
. Because end_ptr - start_ptr
is typed as ptrdiff_t
or similar, it's signed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With