Boost logo

Boost :

Subject: [boost] [optional] layout
From: Domagoj Saric (domagoj.saric_at_[hidden])
Date: 2009-10-01 09:14:30


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 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)...
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...

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)...

--
 "That men do not learn very much from the lessons of history is the most
important of all the lessons of history."
 Aldous Huxley 

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