[Bug c/83584] "ISO C forbids conversion of object pointer to function pointer type" -- no, not really

txr at alumni dot caltech.edu gcc-bugzilla@gcc.gnu.org
Wed Dec 27 07:11:00 GMT 2017


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83584

Tim Rentsch <txr at alumni dot caltech.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |txr at alumni dot caltech.edu

--- Comment #13 from Tim Rentsch <txr at alumni dot caltech.edu> ---
(In reply to Andrew Pinski from comment #11)
> So again this is not a bug.

I mostly agree, but I think some clarification is needed.  For reference
here is the diagnostic gcc gives (under -pedantic-errors)

   error: ISO C forbids conversion of object pointer to function pointer

This text is wrong because it is factually incorrect.  There is nothing
in the ISO C standard that forbids or prohibits converting an object
pointer type to a function pointer type, or vice versa.

The catch is that these conversions aren't required to be supported
either.  Section 6.3 discusses conversions, with section 6.3.2.3
specifically concerned with pointer types.  Each case involving converting
a type to or from a pointer type is listed with a specific permission, eg,
"Any pointer type may be converted to an integer type."  There is no such
statement that says object pointer types may be converted to function
pointer types, or vice versa.  We may reasonably infer from these
statements that the "may be converted" cases must be accepted by all
conforming compilers, but not the others.  To say this the other way, any
case not explicitly covered (involving a pointer type) /may/ be accepted
but is not required to be.  Of course if such a conversion is evaluated
its behavior is undefined, but I mean something else, namely, whether a
program is accepted if it contains any such conversion, whether it is
evaluated or not.  To me it seems clear that the ISO C standard means that
such a program doesn't have to be accepted:  it /can/ be accepted, but it
doesn't /have/ to be.

Note section 4 paragraph 5:  "A /strictly conforming program/ shall use
only those features of the language and library specified in this
International Standard."  Converting between object pointer types and
function pointer types is not covered in the Standard.  I don't mean the
behavior isn't defined;  I mean the case is not addressed, ie, it is not a
feature of the language.  Hence any program containing such a conversion
is not a strictly conforming program, and as such it can be rejected.  So
I believe gcc is within its rights to make this an error.  It would be
nice if the diagnostic text were better, and consistent with what the
Standard says, but as far as being an error goes I think gcc is in the
clear here.


More information about the Gcc-bugs mailing list