Boost logo

Boost :

Subject: Re: [boost] How do I specify no padding bytes within a struct/class?
From: Daryle Walker (darylew_at_[hidden])
Date: 2013-06-20 01:36:23

-----Original Message-----
From: Jonathan Wakely
Sent: Tuesday, June 18, 2013 5:28 AM

>On 17 June 2013 22:30, Daryle Walker wrote:
>> 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 want the code to work on non-sane implementations too, when possible. A
common case that might happen is when the element size is less than a word
("char," for example). The struct may be padding-filled to reach a word

>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

I don't want a hard error. I've actually implemented a "constexpr" class
constant to indicate if a particular class has padding. I was surprised
that I could use an expression with "sizeof(MyClass)" within MyClass. I
experimented and found out the compiler was smart about it; it won't
complain when using the size of a class within that class if it's for
something class-static or otherwise won't affect itself; using something
like a non-static member "char x[sizeof(MyClass)]" does flag an error.

Daryle W.

Boost list run by bdawes at, gregod at, cpdaniel at, john at