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++


Joe Buck <jbuck@synopsys.com> writes:

| I wrote:
| > | It seems to me that there are four categories:
| > | 
| > | 1) Those cases where ISO C++ specifies what the answer is.
| > | 
| > | 2) Those cases where any reasonable implementation (and all
| > |    implementations we know of) give the right answer, but ISO
| > |    C++ says "undefined".
| > | 
| > | 3) Those cases where we know that g++ will give the right answer,
| > |    but we can't be sure of other possible implementations.
| > | 
| > | 4) Those cases where use of offsetof() is nonsense, since the offset
| > |    is not even constant.
| 
| On Thu, Nov 06, 2003 at 08:47:19PM +0100, Gabriel Dos Reis wrote:
| 
| > I don't know where you put my favorite example that involve
| > references:
| > 
| >    int i;
| > 
| >    struct A {
| >      int& r;
| >      A() : r(i) { }
| >    };
| > 
| >    int main()
| >    {
| >      cout << offsetof(A, r) << endl;
| >    }
| 
| Case 4 (it will do the wrong thing).

Well, I can see reasons why it does not fit in that category.  The
compiler is computing the wrong answer not because the question does
not make sense, but just because the compiler has an erroneous
implementation of offsetof.  See the various PRs we have about it.

  Sure there is no requirement that a reference "exists" in A.  But as
a matter of fact, it does exists in form of a pointer in GCC
implementation.  And in nearly all cases, in other implementations, it
does exist in one form or the other.  The offset of that incanartion
is constant.

-- Gaby


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