Boost logo

Boost :

From: Philippe A. Bouchard (philippeb_at_[hidden])
Date: 2002-08-15 11:49:32


"David Abrahams" <dave_at_[hidden]> wrote:

> From: "Philippe A. Bouchard" <philippeb_at_[hidden]>
>
>
> > Here is ptr<> nearly fully portable:
> > http://groups.yahoo.com/group/boost/files/ptr/
> >
> >
> > Now:
> >
> > - 5.7.6 footer 21:
> > "Another way to approach pointer arithmetic is first to convert the
> > pointer(s) to character pointer(s): In this scheme the integral value
of
> > the expression added to or subtracted from the converted pointer is
first
> > multiplied by the size of the object originally pointed to, and the
> > resulting pointer is converted back to the original type. For pointer
> > subtraction, the result of the difference between the character pointers
> is
> > similarly divided by the size of the object originally pointed to."
> >
> > therefore class offset is perfectly legal.
>
> 1. Footnotes are "non-normative", so it doesn't mean anything
> 2. Even if footnotes weren't "non-normative", the above wouldn't prove
> anything, since there's no "pointer arithmetic" involved in converting
> between pointer-to-derived and pointer-to-base

Given:

5.3.3.1:
- "The sizeof operator yields the number of bytes in the object
representation of its operand."
- "sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1."

5.2.10.7:
- "Except that converting an rvalue of type "pointer to T1" to the type
"pointer to T2" (where T1 and T2 are object types and where the alignment
requirements of T2 are no stricter than those of T1) and back to its
original type yields the original pointer value"

struct T { ... };
struct U : T { ... };

k = ptrdiff_t((char *) (T *) (U *) 1 - (char *) 1)
K is some constant

Temporarily unspecified:
m_ptr = (char *) (U *) value - (char *) k

When dereferenced:
m_ptr = (U *) ((char *) (U *) value - (char *) k + (char *) k) Char
alignment is less stricter than U
        = (U *) ((char *) (U *) value - (char *) 0)
        = (U *) ((char *) (U *) value)
        = (U *) value
5.2.10.7 applies

Philippe A. Bouchard


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