简体   繁体   English

C 中函数 typedef 的前向声明

[英]Forward declaration of a function typedef in C

I have a nagging suspicion that this has been asked before, but I'm not finding it...我一直怀疑以前有人问过这个问题,但我没有找到......

Suppose I have a typedef for function A that takes a pointer to a function with typedef B, which in turn takes a function of typedef A. If one of these was a struct, I know how I would handle the forward declaration, but for functions I don't know the syntax.假设我有一个函数 A 的 typedef,它接受一个指向带有 typedef B 的函数的指针,后者又接受一个 typedef A 的函数。如果其中一个是结构,我知道我将如何处理前向声明,但对于函数我不知道语法。 Is there one?有吗?

I want to be able to do:我希望能够做到:

typedef void (*function_A_t)(function_B_t f_B);
typedef void (*function_B_t)(function_A_t f_A);

Any hints?任何提示? Even better, a reference?更好的是,参考? Incidentally this actually just happened for me, but I was able to fix it another way, although this would actually smoother (better decoupling, less chance of next guy messing it up) if it's possible.顺便说一句,这实际上只是发生在我身上,但我能够以另一种方式修复它,尽管这实际上会更顺畅(更好的解耦,下一个人搞砸的机会更少),如果可能的话。

You can do this by taking advantage of the fact that C specifies that a function declaration with no arguments means it takes an indeterminate number of arguments.您可以利用以下事实来做到这一点:C 指定没有参数的函数声明意味着它需要不确定数量的参数。

So you could do it as follows:所以你可以这样做:

typedef void (*function_A_t)(void (*)());
typedef void (*function_B_t)(function_A_t f_A);

Which allows the following to compile:这允许以下编译:

void A(function_B_t b)
{
    b(A);
}

void B(function_A_t a)
{
    a(B);
}

int main()
{
    function_A_t a = A;
    function_B_t b = B;
    a(B);
    b(A);
    return 0;
}

Section 6.7.6.3p15 of the C standard states the following regarding the compatibility of function types: C 标准的第 6.7.6.3p15 节陈述了以下关于函数类型兼容性的内容:

For two function types to be compatible, both shall specify compatible return types.对于要兼容的两个函数类型,都应指定兼容的返回类型。 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.) (在确定类型兼容性和复合类型时,以函数或数组类型声明的每个参数视为具有调整类型,每个以限定类型声明的参数视为具有其声明类型的非限定版本。)

The part in bold above specifies that void (*)() is compatible with void (*)(function_B_t)上面粗体部分表示void (*)()void (*)(function_B_t)兼容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM