[PATCH] Re: [rfc] __builtin_offsetof for c and c++

Andrew Pinski pinskia@physics.uc.edu
Sat Feb 21 13:45:00 GMT 2004


On Feb 8, 2004, at 22:09, Andreas Jaeger wrote:
> At least this does not look correct, you need to add 2004 and not
> 2003.
>
> Please check that all files that you touch have 2004 in it.
>
>
> Do you have any testcases?


There are already some testcases in the testsuite for this.

Here is one which currently fails with the current way but passes with
this new way:

// { dg-run }
// { dg-options "-O2" }

#include <stddef.h>

#define assert_link_error(a) ((void) ((a) ? 0 : link_error ()))

int link_error(void);

struct X { int ary[38]; };

struct A {
     template<int N, class T>
       int offset(T*) { return offsetof(typename T::X, ary[N]); }

     template<int N>
       int offset() { return offsetof(X, ary[N]); }

       int offset() { return offsetof(X, ary[34]); }
};

struct B { typedef ::X X; };

int main()
{
     A a;
     assert_link_error(a.offset<34>((B*)0) == a.offset<34>());
     assert_link_error(a.offset<34>((B*)0) == a.offset());
     assert_link_error(a.offset<34>() == a.offset());
}



More information about the Gcc-patches mailing list