From: requinham on
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.

my problem is that i don't found the way to get the size of structure
in preprocessor mode and it's necessary to generate a COMPILE ERROR
when the size of my struct is not pow of 2 :(

thank you

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

From: red floyd on
On Mar 4, 12: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.
>
> my problem is that i don't found the way to get the size of structure
> in preprocessor mode and it's necessary to generate a COMPILE ERROR
> when the size of my struct is not pow of 2 :(

You can't... at least not in the preprocessor -- it does pure text
substitution.

There's probably some template magic you could do. My template-fu
is weak, but you probably want something along the lines of this
(any template gurus feel free to help out here):


template<int N>
struct sizer_t { enum { val = -1 }; };

template<>
struct sizer_t<1> {enum {val = 1 }; };

template<>
struct sizer_t<2> {enum {val = 1 }; };

// lather/rinse/repeat for all powers of 2

template <typename T>
struct size_helper {
static char foo[sizer_t<sizeof(T)>::val];
};
template <typename T>
char size_helper<T>::foo[sizer_t<sizeof(T)>::val];



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

From: analizer on
On 4 ���, 23:09, requinham <requin...(a)gmail.com> wrote:
> my problem is that i don't found the way to get the size of structure
> in preprocessor mode and it's necessary to generate a COMPILE ERROR
> when the size of my struct is not pow of 2 :(

try this:

struct s
{
/* some structure definition here */
};

int size_of_s_is_not_a_pow_of_2[sizeof(struct s) & (sizeof(struct s) -
1) ? -1 : 1]; /* if size of s is not a pow of 2, array size will be
-1 which won't be accepted by compiler */

int main()
{}

I've tested this using gcc and como, works both on C and C++.
And don't forget about alignment.

----
Dmitry


--
[ 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 4 Mar, 20:09, 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.
>
> my problem is that i don't found the way to get the size of structure
> in preprocessor mode and it's necessary to generate a COMPILE ERROR
> when the size of my struct is not pow of 2 :(

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.

e.g.

struct S1
{
short s; // assume 2 bytes
char c;
};

S1 sa[2];

Although this can theoretically be packed in 3 bytes. The alignment
requirement for sa[1].s means that it has to be rounded up to 4.
If you have virtual methods or any pointer then it will almost
certainly be rounded to pointer alignment.

Yes there are archtectures that support unaligned access but it is
invariably much less efficient so all compilers will always generate
layouts for aligned array access.

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

From: Marco Nef on
> 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.

For a solution of your problem I'm recommending to do some template meta
programming. See Alexandrescu's "Modern C++ Design" for some ideas.

Marco


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