Boost logo

Boost :

From: Philippe A. Bouchard (philippeb_at_[hidden])
Date: 2002-08-20 14:10:09


"David B. Held" <dheld_at_[hidden]> wrote:
> "Philippe A. Bouchard" <philippeb_at_[hidden]> wrote:
> > - operator +/- (..., offset) now returns char * (less error-prone);
> > - ptr_base<T, __false_type>::m_ptr is of type void * to allow perfect
> > copies;
>
> Not sure what this means.

- operator +/- is less error-prone because reinterpret_cast<>s would have to
be explicit, therefore the pointers will follow rules and regulations of
alignments.
- element_type not aligned to its type is not specified in the standard even
though it is not dereferenced.

> > - all reinterpret_cast<>s are justified.
>
> I don't think they need to be justified. It's perfectly clear why they
are
> used. They need to be *tested* to assure that they work properly on
> all intended platforms (if the only intended platform is gcc 2.9x, then
> say so, but don't say that it's 98% portable when you've only tested
> on one platform).

I follow the already tested *rules*.

> I believe that your implementation of pos() is not valid C++ code:
>
> template <class C, typename T>
> inline offset pos(T C::* p)
> {
> return size_t(& (reinterpret_cast<C *>(0)->*p));
> }
>
> In particular, I believe it is invalid to dereference a null pointer, even
> just to compute a member offset. I believe there is an offsetof macro
> that does this in a correct way. Try:
>
> #include <cstddef>
>
> member_offset = offsetof(C, p);

Yeah I know, I've just noticed it also. I've noticed also that the
implementation is the same with gcc's.

> Finally, do you have a problem with Larry's implementation of the
> conversion c'tor?
>
> template <typename U>
> ptr_base(ptr_base<U, __false_type> const & a_p) : m_ptr(a_p.share())
> {
> if (s_offset.size() <= ptr_base<U, __false_type>::s_id)
> {
> s_offset.resize(ptr_base<U, __false_type>::s_id + 1);
> }
>
> char c[sizeof(U)];
> U* u(reinterpret_cast<U*>(c));
> T * p(u);
> s_offset[ptr_base<U, __false_type>::s_id] = size_t(p - offset(u));
> }
>
> It seems like it ought to be just as fast, and avoids the ugly pU(1)
> construct (which I think happens to stink ;).

Because convertions from char * to U * is unspecified. I simply forgot to
mention it. Sorry.

> Dave

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