This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: On alignment
- From: Jason Merrill <jason at redhat dot com>
- To: Gabriel Dos Reis <gdr at integrable-solutions dot net>
- Cc: tromey at redhat dot com, Andrew Haley <aph at redhat dot com>, Jamie Lokier <jamie at shareable dot org>, gcc at gcc dot gnu dot org
- Date: Mon, 05 May 2003 10:55:03 -0400
- Subject: Re: On alignment
- References: <200303251122.13693.kevin.hendricks@sympatico.ca><wvl1y0vmhdv.fsf@prospero.boston.redhat.com><wvlu1drl2kl.fsf@prospero.boston.redhat.com><200303251344.59988.kevin.hendricks@sympatico.ca><wvlhe9rkzgm.fsf@prospero.boston.redhat.com><16037.6826.35777.756256@cuddles.redhat.com><jen0ii4x27.fsf@sykes.suse.de><20030423124944.GA24593@mail.jlokier.co.uk><87wuhlgq5l.fsf@fleche.redhat.com><wvlu1cpdv7s.fsf@prospero.boston.redhat.com><87lly1f5ir.fsf@fleche.redhat.com><wvllly1dq6q.fsf@prospero.boston.redhat.com><m3ist5auzc.fsf@uniton.integrable-solutions.net><87sms9dmuy.fsf@fleche.redhat.com><wvlptncd0i2.fsf@prospero.boston.redhat.com><16039.44856.826662.680167@cuddles.redhat.com><87znm6utca.fsf@fleche.redhat.com><m3llxpo5sl.fsf@uniton.integrable-solutions.net>
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