Boost logo

Boost :

Subject: Re: [boost] [optional] layout
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-10-01 13:58:54


AMDG

Domagoj Saric wrote:
> why is boost::optional laid out so that the bool comes before the object's
> placeholder/storage?
>
> this takes up to 7 extra bytes of storage with default alignement

These two structs should both take up 16 bytes.
You don't save anything by reordering the
members.

struct s1 {
    bool initialized;
    double value;
};

struct s2 {
    double value;
    bool initialized;
};

> and, more
> importantly, it forces pointer offset adjustment/calculation when accessing
> a boost::optional<> through a pointer (and trying to access the actual
> contained object)...
>

Is the difference in performance actually measurable in any way?

> it also makes life harder for those of us keen to hackery ;) because it
> makes the address of an optional different from the address of the actual
> object within the optional...
>

Um, you shouldn't rely on this anyway. The standard only
makes guarantees about the addresses of sub-objects for
POD types.

> ps. it would be 'cool' if all of the functionality from boost::optional<>
> not related to the 'is initialized' bool (conditional destruction, etc...)
> could be extracted into a separate class (usefull for cases where you have
> to delay/in-place construct an object but are certain that you will
> construct it by the point where it needs to be used or destroyed...so you do
> not need the 'conditional' overhead)...
>

In Christ,
Steven Watanabe


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