using offsetof with g++
Trevor Blackwell
tlb@anybots.com
Fri Jun 4 03:29:00 GMT 2004
> If you can persuade the C++ committee that offsetof is important not
> only for legacy C code but also for new C++ code, then there's a good
> chance that this restriction will be loosened in a future version of the
> language. On the other hand, you can also expect to be asked why
> you want to use offsetof instead of pointers-to-member. (And if you can
> show that there are things you can't do with pointers to members, you
> might find that people will prefer to solve the problem by removing
> whatever limitation pointers to members have.)
I've been having the same problem with offsetof. The thing I want to do
that can't be done with member pointers is:
struct B {
int y;
};
struct A {
int x;
B b;
};
int A::*myptr1 = &A::x; // fine
int A::*myptr2 = &A::b.y; // error
that is, I want to refer to something in a near-POD nested in a
near-POD. The nested design is mainly to break up a large namespace.
With normal data layout, this is a reasonable thing to do. It works
correctly with offsetof except for the warning.
g++-3.3.4 doesn't have the advertised -Wno-invalid-offsetof. So, my
workaround is:
A *dummy=0;
int myofs=&(char *)dummy->b.y - (char *)dummy;
which isn't the most elegant solution.
--
Trevor Blackwell tlb@anybots.com (650) 210-9272
More information about the Gcc
mailing list