From: CornedBee on
On Mar 4, 9:09 pm, requinham <requin...(a)gmail.com> wrote:
> Hello,
>
> i have a structure in my program and i want generate a compile error
> when the size of structure is not pow of two.

Use Boost.

#include <boost/static_assert.hpp>
BOOST_STATIC_ASSERT((sizeof(mystruct) & (sizeof(mystruct) - 1)) == 0);

Essentially, BOOST_STATIC_ASSERT compiles down to this:
typedef char assertion[condition ? 1 : -1];
with some tricks thrown in to avoid name redefinitions and compiler
quirks.

Or if your compiler is new enough, use static_assert.

static_assert((sizeof(mystruct) & (sizeof(mystruct) - 1)) == 0,
"Size of struct is not a power of two.");


That said, this is a weird requirement. Why would you need the size to
be a power of two?


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Nick Hounsome on
On 5 Mar, 14:22, "Marco Nef" <maill...(a)shima.ch> wrote:
> > Probably not worth the effort.
> > On every compiler and architecture that I've ever worked on almost all
> > structures will be at least 2 (or even 4 byte) aligned. This is
> > because the alignment is always at least enough as is needed
> > to put them in an array. This means that the size is rounded up to at
> > least the alignment of the first member.
>
> But sometimes machine word alignment is not enough, e.g. SSE2 which has to
> be 16 bytes aligned.

Whatever.

If an architecture requires a minimum of N byte alignment then
everything must be rounded up to a multiple of N or arrays wont work.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jens Schmidt on
Nick Hounsome wrote:

> On 5 Mar, 14:22, "Marco Nef" <maill...(a)shima.ch> wrote:
>> But sometimes machine word alignment is not enough, e.g. SSE2 which has
>> to be 16 bytes aligned.
>
> If an architecture requires a minimum of N byte alignment then
> everything must be rounded up to a multiple of N or arrays wont work.

Or the language implementer decides the feature to be so rare and obscure
that neither compiler nor library will use it. Here the alignment problem
is left as an exercise to the user.
Or special code will align variables with special attributes more strictly
than those without.
Special alignment requirements can always be satisfied by over-allocating
by (special_requirement - standard_alignment) and then using a properly
aligned subregion only.
--
Greetings,
Jens Schmidt


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Nick Hounsome on
On 6 Mar, 13:57, Jens Schmidt <Jens.Schmidt...(a)gmx.de> wrote:
> Nick Hounsome wrote:
> > On 5 Mar, 14:22, "Marco Nef" <maill...(a)shima.ch> wrote:
> >> But sometimes machine word alignment is not enough, e.g. SSE2 which has
> >> to be 16 bytes aligned.
>
> > If an architecture requires a minimum of N byte alignment then
> > everything must be rounded up to a multiple of N or arrays wont work.
>
> Or the language implementer decides the feature to be so rare and obscure
> that neither compiler nor library will use it. Here the alignment problem
> is left as an exercise to the user.

Arrays are rare???????

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jens Schmidt on
Nick Hounsome wrote:

> On 6 Mar, 13:57, Jens Schmidt <Jens.Schmidt...(a)gmx.de> wrote:
>> Or the language implementer decides the feature to be so rare and obscure
>> that neither compiler nor library will use it. Here the alignment problem
>> is left as an exercise to the user.
>
> Arrays are rare???????

Not arrays as such, but arrays that are intended to be processed with SSE2.
Especially if the compiler doesn't generate SSE2 instructions.
--
Greetings,
Jens Schmidt


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]