From: CornedBee on 4 Mar 2010 21:35 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 5 Mar 2010 02:25 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 5 Mar 2010 20:57 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 6 Mar 2010 15:31 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 7 Mar 2010 05:00 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! ]
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: operator overloading == Next: invalid initialization of non-const reference of type |