invalid offsetof from non-POD type

John Quigley johnw@lowestplane.org
Tue Apr 22 04:22:00 GMT 2003


On Monday 21 April 2003 7:34 pm, Gabriel Dos Reis wrote:
> John Quigley <johnw@lowestplane.org> writes:
> | While it is not standards compliant code, gcc still provides the correct
> | result.
>
> The key issue is what do you define to be the "correct" result when you
> apply offsetof() to a non-POD?
>
> -- Gaby

This isn't a precise definition, but I would expect that I can use offsetof to 
compute the offset of a field of a non-POD type.  Then I can take a pointer 
to an instance of that non-POD, advance it by adding the offset, and the new 
pointer will be addressing the desired field in the instance.

Perhaps whoever wrote the code in gcc has a real definition, since they seemed 
to think that they were producing the right answer:

  /* Complain about other invalid uses of offsetof, even though they will
     give the right answer.  Note that we complain whether or not they
     actually used the offsetof macro, since there's no way to know at this
     point.  So we just give a warning, instead of a pedwarn.  */
  if (protect
      && CLASSTYPE_NON_POD_P (old_basetype)
      && TREE_CODE (old_datum) == INDIRECT_REF
      && integer_zerop (TREE_OPERAND (old_datum, 0)))
    warning ("\
invalid offsetof from non-POD type `%#T'; use pointer to member instead",
	     basetype);



More information about the Gcc-bugs mailing list