This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: using offsetof with g++


> 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



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]