Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-02-11 07:59:26


"Philippe A. Bouchard" <philippeb_at_[hidden]> writes:

> Hi again,
>
> I am doing some helper class that will pad the space after a given type
> until it reaches a machine word boundary. This word boundary would be the
> maximum of (alignment_of<T>::value - sizeof(char)) where T is any
> 'typename', 'typename' is one of {is_class<U>::value == false} and U is any
> type.

Even if what you wrote made sense, which I'm not sure it does,

     [to me at least - how can a word boundary be a number (max over
      all types T of alignment_of<T> - sizeof(char)), rather than an
      address? And then why subtract sizeof(char), i.e. 1, from the
      maximal alignment?]

that algorithm relies on a big non-portable assumption, doesn't it?
Even if you enumerated all non-class types, there's no reason to think
that the maximal alignment has any relationship to a machine word.

> I would like some help to isolate the most effective way to
> define BOOST_DATA_ALIGNMENT in the following example (defined by
> default to 8 -- based on std headers).

Well, first of all, not ALL_CAPS; that should be reserved for macros.

> I would like to propose padding_of<> at the same time.
>
> template <typename T>
> struct padding_of
> {
> size_t const BOOST_DATA_ALIGNMENT = 8;
>
> size_t const value = (BOOST_DATA_ALIGNMENT - sizeof(T) %
> BOOST_DATA_ALIGNMENT) % BOOST_DATA_ALIGNMENT;
> };
>

Since I can't figure out what any of this is actually supposed to
mean, I guess I can't help with that.

> template <typename T>
> struct padded
> {
> T type;
> char padding[padding_of<T>::value]; // can be of size 0!

zero-length arrays are not legal in C++.

> };
>
> template <typename T>
> struct shifted_object : padded<counted_base>
> {
> T value;
> };

Sorry I couldn't help more,

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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