为什么不能使用函数的typedef定义函数? [英] Why can't a typedef of a function be used to define a function?
问题描述
从ISO / IEC 14882:2011(E)的第8.3.5.11节:
函数类型的typedef可用于声明一个函数,但不能用于定义函数
标准接下来给出这个例子:
typedef void F();
F fv; // OK:等效于void fv();
F fv {} //形成的
void fv(){} // OK:fv的定义
这个规则有什么动机?虽然这个问题是关于C ++的,但是因为C ++继承了<$
c $ c> typedef 和从C的函数指针,所以在C中可以使用相同的问题的解释。有对C的正式解释。
国际标准 - 编程语言的基本原理C §6.9.1函数定义
参数列表必须显式地出现在声明器中;它不能继承自
typedef
(见§6.7.5.3)。也就是说,给定定义:typedef int p(int q,int r);
以下片段无效:
p funk // weird
{return q + r; }
某些当前实现重写类型,例如
char
参数,因为该参数已知为以int
在没有原型的情况下。然而,该标准要求接收到的参数在函数输入时通过赋值被转换。因此,不再允许类型重写。
From § 8.3.5.11 of ISO/IEC 14882:2011(E):
A typedef of function type may be used to declare a function but shall not be used to define a function
The standard goes on to give this example:
typedef void F();
F fv; // OK: equivalent to void fv();
F fv { } // ill-formed
void fv() { } // OK: definition of fv
What motivates this rule? It seems to limit the potential expressive usefulness of function typedefs.
Though this question is about C++, but since C++ inherits typedef
and function pointer from C, so an explanation of the same question in C can be used in here. There's a formal explanation for C.
Rationale for International Standard - Programming Languages C §6.9.1 Function definitions
An argument list must be explicitly present in the declarator; it cannot be inherited from a
typedef
(see §6.7.5.3). That is to say, given the definition:typedef int p(int q, int r);
the following fragment is invalid:
p funk // weird { return q + r ; }
Some current implementations rewrite the type of, for instance, a
char
parameter as if it were declaredint
, since the argument is known to be passed as anint
in the absence of a prototype. The Standard requires, however, that the received argument be converted as if by assignment upon function entry. Type rewriting is thus no longer permissible.
这篇关于为什么不能使用函数的typedef定义函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!