Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-03-01 10:28:39


----- Original Message -----
From: "Douglas Gregor" <gregod_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, March 01, 2002 10:09 AM
Subject: Re: [boost] Re: New version of valunion and stack_allocator
uploaded

> On Friday 01 March 2002 09:06 am, you wrote:
> > > The type to align against is a union, where the type 'X' of each
member
> > > is
> >
> > a
> >
> > > type 'U' if sizeof(U) <= sizeof(type_we_want_to_make_storage_for) and
> > > char otherwise.
> >
> > So if the type I'm storing is char[6], it will over-align, yes?
>
> Yes. That's not a fault of the approach, but of the implementation.
>
> > In my application it's much more important to actually produce an
> > appropriate alignment than that the size is minimal, but that won't be
true
> > for many. Since this is all a crapshoot anyway and the language gives no
> > guarantees of success it seems like Brian's algorithm ought at least to
> > check that sizeof(U) <= alignment_of(type_we_want_to_make_storage_for).
> >
> > -Dave
>
> Ok, but I still like Brian Parker's design better, and it's easy to change
> the algorithm.

I'm not arguing!

> Here's his code, that I slightly modified to do as you
> mentioned above:
>
> template<typename T, size_t sz, size_t align>
> struct member {
> typedef typename ct_if<sizeof(T) <= sz && sizeof(U) <= align,
> T,
> char>::type type;
> };
>
> template<size_t sz, size_t align>
> union force_align {
> typename member<short, sz, align>::type a;
> typename member<int, sz, align>::type b;
> typename member<long, sz, align>::type c;
> typename member<float, sz, align>::type d;
> typename member<double, sz, align>::type e;
> typename member<long double, sz, align>::type f;
> typename member<void*, sz, align>::type g;
> typename member<ALIGN_FPTR, sz, align>::type h;
> typename member<POD, sz, align>::type i;
>
> // todo: add any other built-in or POD types with stricter
> // alignment restrictions here
> };
>
> No typelist, no compile-time for_each, and only one trivial
metaprogramming
> construct used (ct_if).

Nice. Shouldn't we also be checking that the alignment of T is <= align,
just to be sure?

-Dave


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