隐式函数指针转换 [英] Implicit function-pointer conversions

查看:173
本文介绍了隐式函数指针转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据tcc,gcc和clang的经验,指向 返回RetTp的旧函数和任何原型的指针 返回RetTp的函数可以相互隐式转换:

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;

C标准中是否有任何东西可以保证这种行为,或者仅仅是扩展?

Is there anything in the C standard that guarantees such behavior or is it just an extension?

推荐答案

可以保证转换的隐式性.

The implicitness of the conversion is guaranteed.

6.5.16.1p1 指出如果(除其他事项外)"...两个操作数都是指向兼容类型的合格或不合格版本的指针..." ("...,以及左侧所指向的类型,则赋值是有效的具有右侧所指向类型的所有限定符").

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 使该功能类型兼容:

要使两种功能类型兼容,都应指定 兼容的返回类型.146)此外,参数类型列出(如果 两者都存在,应在参数数量和使用上达成一致 省略号的终止符;相应的参数应具有 兼容类型.如果一种类型具有参数类型列表,而另一种具有参数类型列表 类型由不属于函数声明符的函数声明符指定 函数定义,其中包含一个空的标识符列表, 参数列表中不得包含省略号和类型 每个参数应与由 应用默认参数提升.如果一种类型具有 参数类型列表,其他类型由函数指定 包含(可能为空)标识符列表的定义,两者 应在参数数量和每个参数的类型上达成共识 原型参数应与结果类型兼容 从默认参数提升的应用到 相应的标识符. (在确定类型 兼容性和复合类型,每个参数都用 函数或数组类型被视为具有调整后的类型,并且每个 以限定类型声明的参数被视为具有 声明类型的非限定版本.)

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.)

这篇关于隐式函数指针转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆