using offsetof with g++
Gabriel Dos Reis
gdr@integrable-solutions.net
Fri Jun 4 03:41:00 GMT 2004
Trevor Blackwell <tlb@anybots.com> writes:
| > 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
B* A::*myptr2 = &A::b; // fine
there has been a proposal to allow composition of point-to-member and
generic pointer to data member. And there still is a suggestion
to complete the set of operations on pointer to members.
myptr2 is an index into B. So if you get that index, then you can
also compose &A::b with &B::y and get the relative of B::y inside A.
-- Gaby
More information about the Gcc
mailing list