In std::ssize, why return the common_type of decltype(c.size()) and ptrdiff_t?

Viewed 85

I wanted a signed type corresponding to std::size (ideally computed from it rather than relying on another definition that could be independent.) Naturally I thought to use std::make_signed_t<std::size_t>.

However, when I looked at std::ssize, I noticed the return type was std::common_type_t of std::ptrdiff_t and decltype(c.size()). Now c.size() should be unsigned, but in either case, it should become signed because std::ptrdiff_t is. All that's left is the size; that should be the larger of the two types.

What I don't get is why you would use a large type like std::ptrdiff_t as if you wanted the largest signed type. If it really were the largest, there would be no point to the std::common_type_t. Instead, assuming std::common_type_t doesn't always return std::ptrdiff_t, that means decltype(c.size()) is a different size, and effectively std::ptrdiff_t is used for a minimum-size signed type. And if that's the case, shouldn't the smallest signed type be used instead? Or, why wouldn't the signed type corresponding to decltype(c.size()) be used directly? What could be the intent behind that definition?

0 Answers
Related