milleniumbug
milleniumbug

Reputation: 15814

Why does proposed `std::shared_ptr::operator[]` take `std::ptrdiff_t` as an argument

According to the N4562 proposal, the newly proposed std::shared_ptr::operator[] takes in std::ptrdiff_t, which is a signed type.

This is inconsistent with every indexing operator in standard library. Even std::unique_ptr::operator[] takes std::size_t.

What's the rationale for this decision?

Upvotes: 14

Views: 418

Answers (1)

bipll
bipll

Reputation: 11940

Probably this should be pointer interface unification. Good ol' C pointers when used as arrays accept negative indices: p[-2] is the same as *(p - 2); and ptrdiff_t is thus naturally signed.

Upvotes: 1

Related Questions