$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [boost] How do I specify no padding bytes within a struct/class?
From: Jonathan Wakely (jwakely.boost_at_[hidden])
Date: 2013-06-18 05:28:02
On 17 June 2013 22:30, Daryle Walker wrote:
>> If you don't mind, why do you want to do pack structs in the
>> first place? Most people cringe at the thought of packing-specific
>> code.
>
> I've read about using an array segment of complex numbers as an array of real numbers (but twice as long) for stuff like Fourier transforms. At http://en.cppreference.com/w/cpp/numeric/complex#Non-static_data_members, the effect is documented for std::complex (at least for float/double/long-double). I want to simulate the effect.
>
> template < typename T, unsigned R >
> struct complex_it {
> T c[ 1ULL << R ];
> };
>
> The above class template will be standard-layout if "T" is; that mandates no starting padding. Array elements are packed. So I can do the array-segment translation only if there's no trailing padding. In contrast:
>
> template < typename T, unsigned R > struct complex_rt;
>
> template < typename T >
> struct complex_rt<T, 0u> { T r; };
>
> template < typename T, unsigned R >
> struct complex_rt { complex_rt<T, R - 1u> b[2]; };
>
> will have padding all over the place if there's any trailing padding at a lower level.
But why would it have padding at a lower level?
On a sane implementation complex_rt<T,0> will have the same layout as
T, so an array of them will have the same layout as an array of T, ...
*unless* you start dicking about with alignment and padding by
overriding the compiler's decisions, at which point you might break
things.
I agree that it would be better to simply use a static_assert to check
the layout meets your requirements, and only force something different
if needed.