Boost logo

Boost :

From: Gabriel Dos Reis (gdr_at_[hidden])
Date: 2002-08-28 00:42:58


"Peter Dimov" <pdimov_at_[hidden]> writes:

| From: "Gabriel Dos Reis" <gdr_at_[hidden]>
| > Andrew Koenig <ark_at_[hidden]> writes:
| >
| > | David> I don't think that's the real question at all.
| > |
| > | David> The problem is that the return type of operator[] can't be relied
| on to be
| > | David> a reference, and in particular you can't do
| > |
| > | David> p[n] = x;
| > |
| > | David> with a generic random-access iterator.
| > |
| > | David> *(p + n) = x;
| > |
| > | David> always works (for sufficiently correct values of n and x).
| > |
| > | Interesting -- I wonder why I never noticed that?
| > |
| > | One would think that p[n] should be equivalent to *(p+n) -- in
| particular
| > | it should be required to have the same type.
| >
| > The random access iterator requirements table 76 says that p[n] as the
| > "operational semantics" "*(p + n)".
|
| We already went over this with Dave, but:

Thanks for the update.

[...]

| p[n] cannot return *(p + n) by reference, since the p + n temporary will
| disappear and the result would be a dangling reference.
|
| That's why p[n] is specified to be "convertible to T" - to allow a proxy
| with operator T() / operator=(T const &) to be returned.
|
| In practice, this means that p[n] should never be used.

More accurately, p[n] should not be allowed to return something
different from *(p + n).

-- Gaby


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