Boost logo

Boost :

Subject: Re: [boost] [smart-ptr][containers] type of the operator[] index
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2011-12-04 12:02:19


On Sunday, December 04, 2011 17:46:02 Vicente J. Botet Escriba wrote:
> Hi,
>
> which is the correct type used as index of operator[]. scoped_array uses
> std::ptrdiff_t. Others containers use std::size_t.
>
> The standard uses also std::size_t, but in
>
> 13.6 Built-in operators
> 13 For every cv-qualified or cv-unqualified object type T there exist
> candidate operator functions of the form
> T* operator+(T*, std::ptrdiff_t);
> T& operator[](T*, std::ptrdiff_t);
> T* operator-(T*, std::ptrdiff_t);
> T* operator+(std::ptrdiff_t, T*);
> T& operator[](std::ptrdiff_t, T*);
>
> we see again std::ptrdiff_t.
>
> When using an index to move from a scoped_array to another container
> which should be the type if the index?

In containers, there isn't any sense in negative indices since they are always
relative to the beginning of the container (and there isn't any elements
before the beginning). This is not true for pointers or iterators since a
pointer may refer to an element in the midst of a valid range.

Smart pointers might depart from raw pointers in this regard because they also
tend to point to the beginning of the range. But as I see it, they also accept
ptrdiff_t to be as close to raw pointers as possible.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk