Boost logo

Boost :

Subject: Re: [boost] How do I specify no padding bytes within a struct/class?
From: John Bytheway (jbytheway+boost_at_[hidden])
Date: 2013-06-17 18:34:53


On 2013-06-17 17: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.

I wouldn't expect any padding in such structures, because at core
they're just a sequence of Ts.

Perhaps you could simply verify no padding by static_assert on the size,
and not worry about it unless that assertion ever fires?

John Bytheway


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