Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2000-08-26 08:07:08


I am not fond of that definition, for several reasons:

template <class T>
class alignment_of
{
   struct padded
   {
      char c;
      T t;
      padded();
   };
public:
   enum{ value = sizeof(padded) - sizeof(T) };
};

1. since padded has a user-defined ctor, it is non-POD, and the compiler is
free to lay it out any way it likes. For example, the definition of T might
be:

struct T { int x; char c; };

If the compiler laid out padded::c right after T::c, alignment would be 0.
What does it mean to have 0 alignment?

2. A compiler which just has a policy of aligning all structs on 4-byte
boundaries (I've seen that) will produce the answer 3 when T is char.

-Dave

----- Original Message -----
From: "John Maddock" <John_Maddock_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Saturday, August 26, 2000 7:09 AM
Subject: Re: [boost] low-level sub-allocation/alignment challenge

> Dave,
>
> >How about this, for determining the alignment requirements of any POD:
>
> In case you hadn't noticed, there is an alignment_of in type_traits.hpp
> already - for exactly this kind of usage as well!
>
> - John.
>
>
>
>


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