Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++17

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?

like image 403
milleniumbug Avatar asked Oct 06 '16 19:10

milleniumbug


1 Answers

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.

like image 134
bipll Avatar answered Nov 02 '22 17:11

bipll