Boost logo

Boost :

From: Marco (mrcekets_at_[hidden])
Date: 2007-10-04 18:13:31


About portability this is what the standard (9.2/12) says :

> Nonstatic data members of a (non-union) class declared without an
> intervening /access-specifier/ are allocated so that later members have
> higher addresses within a class object. The order of allocation of
> nonstatic data members separated by an /access-specifier/ is
> unspecified. Implementation alignment requirements might
> cause two adjacent members not to be allocated immediately after each
> other; so might requirements for space for managing virtual functions
> and virtual base classes.

as recently pointed out to me by Sebastian Redl.

> template <typename T>
> struct point2d {
> T x, y;
> T& operator[](int i) { return (&x)[i]; }
> };

In the struct point2d there is no virtual functions,
between data members there is no intervening access specifier,
so if sizeof(T) is a multiple of the used alignment the standard guarantes
that such code is valid,
and this condition is verified for T = int, long, float, double using the
default alignment (4 bytes).

Regards,
Marco Cecchetti

On Thu, 04 Oct 2007 22:15:33 +0200, Michael Fawcett
<michael.fawcett_at_[hidden]> wrote:

> On 10/4/07, Phil Endecott <spam_from_boost_dev_at_[hidden]> wrote:
>> Michael Marcin wrote:
>> >
>> > Have a look at:
>> >
>> > http://www.gamedev.net/community/forums/topic.asp?topic_id=261920
>>
>> Yes, a very good page showing several solutions:
>>
>> 1. A vector of member pointers (may have some overhead).
>> 2. Anonymous structs and unions (may be non-standard).
>> 3. x, y and z implemented as references (makes object bigger).
>> 4. Relying on (&x)[1] pointing to y.
>
> For actual asm output, see
> http://www.gamedev.net/community/forums/topic.asp?topic_id=230443&PageSize=25&WhichPage=2
>
> for the "overhead" of option 1.
>
>> _IF_ 4 really is guaranteed to work portably (as its author claims), it
>> would get my vote:
>>
>> template <typename T>
>> struct point2d {
>> T x, y;
>> T& operator[](int i) { return (&x)[i]; }
>> };
>>
>> ...what's the catch?
>
> I'm almost positive the author was wrong in claiming that it is
> portable. Here's an old thread about the issue:
>
> http://groups.google.com/group/comp.lang.c++.moderated/browse_frm/thread/df5bc148a994d511/36c133bbb606bece?lnk=gst&q=point+operator%5B%5D+x+y+z&rnum=6#36c133bbb606bece
>
>
> --Michael Fawcett
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

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