On alignment
Jason Merrill
jason@redhat.com
Mon May 5 14:56:00 GMT 2003
On 02 May 2003 15:07:54 +0200, Gabriel Dos Reis <gdr@integrable-solutions.net> wrote:
> Tom Tromey <tromey@redhat.com> writes:
>
> [...]
>
> | What will eventually go wrong with what we have now?
> |
> | template<typename T>
> | struct aligner
> | {
> | T field;
> | };
> |
> | #define ALIGNOF(TYPE) (__alignof__ (((aligner<TYPE> *) 0)->field))
>
> This, as I understand it from my previous attempt, won't give you the
> right answer (for double, it would give 8 instead of 4).
It will give you the right answer on the trunk (and I just checked in a
patch so it will give you the right answer in 3.3 as well).
> What is wrong with the following code I posted earlier?
>
> template<typename T>
> struct alignment {
>
> struct helper {
> T datum;
> helper();
> };
>
> static helper instance;
>
> enum { value = __alignof__ (instance) };
> };
The alignment of a non-field instance of a class can be greater that the
alignment of the class itself. Just saying __alignof (helper) should work,
though I'm not sure it's guaranteed to work. Also, why do you give helper
a constructor?
Tom's example above is directly asking the question they want an answer to;
as long as it works, that's the best way to do it, and it does work now.
Jason
More information about the Gcc
mailing list