问题
Empirically on tcc, gcc, and clang, a pointer to an
old-style function returning RetTp
and a pointer to any prototyped
function returning RetTp
are mutually implicitly convertible to each other:
//compiles without warnings
typedef void RetTp;
RetTp oldfn(){};
RetTp newfn(int X){};
RetTp (*oldfnp)() = newfn;
RetTp (*newfnp)(int X) = oldfn;
Is there anything in the C standard that guarantees such behavior or is it just an extension?
回答1:
The implicitness of the conversion is guaranteed.
6.5.16.1p1 states that an assignment is valid if (among other things) "...both operands are pointers to qualified or unqualified versions of compatible types..." ("..., and the type pointed to by the left has all the qualifiers of the type pointed to by the right").
6.7.6.3p15 makes the function types compatible:
For two function types to be compatible, both shall specify compatible return types.146) Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; corresponding parameters shall have compatible types. If one type has a parameter type list and the other type is specified by a function declarator that is not part of a function definition and that contains an empty identifier list, the parameter list shall not have an ellipsis terminator and the type of each parameter shall be compatible with the type that results from the application of the default argument promotions. If one type has a parameter type list and the other type is specified by a function definition that contains a (possibly empty) identifier list, both shall agree in the number of parameters, and the type of each prototype parameter shall be compatible with the type that results from the application of the default argument promotions to the type of the corresponding identifier. (In the determination of type compatibility and of a composite type, each parameter declared with function or array type is taken as having the adjusted type and each parameter declared with qualified type is taken as having the unqualified version of its declared type.)
来源:https://stackoverflow.com/questions/49847582/implicit-function-pointer-conversions