简体   繁体   English

为什么我可以在另一个函数中定义一个函数?

[英]Why can I define a function in another function?

see the code below, I define a function in another function, 看到下面的代码,我在另一个函数中定义了一个函数,

void test1(void)
{
 void test2(void)
 {
   printf("test2\n");
 }
 printf("test1\n");
}

int main(void)
{
 test1();
 return 0;
}

this usage is odd,is it a usage of c89/c99 or only a extension of gcc (I used gcc 4.6.3 in ubuntu 12 compiled). 这个用法很奇怪,是用c89 / c99还是只用gcc的扩展名(我在ubuntu 12编译时使用了gcc 4.6.3)。 I run this code and it output "test2" and "test1".test2 can be only called in test1. 我运行此代码并输出“test2”和“test1”.test2只能在test1中调用。

What's more,what's the common scene of this usage or what does this usage used for? 更重要的是,这种用法的常见场景是什么,或者这种用法用于什么?

Yes, this is a GCC extension . 是的,这是GCC扩展

It's not C, it's not portable, and thus not very recommended unless you know that GCC will 这不是C,它不是便携式的,因此不是非常推荐,除非你知道GCC会

  • Be the only compiler used to build your code 是唯一用于构建代码的编译器
  • Will keep supporting this feature in future versions 将来版本将继续支持此功能
  • Don't care about principle of least astonishment . 不要关心最不惊讶的原则

As written, it's not legal C++. 如上所述,它不是合法的C ++。 You can, however, define a class within a function, and define functions in that class. 但是,您可以在函数中定义类,并在该类中定义函数。 But even then, in pre C++11, it's still only lexical nesting; 但即便如此,在C ++ 11之前,它仍然只是词汇嵌套; the class you define does not "capture" any of the context of the outer function (unless you implement the capture explicitly); 你定义的类不会“捕获”外部函数的任何上下文(除非你明确地实现了捕获); in a true nested function, the nested function can access local variables in the outer function. 在真正的嵌套函数中,嵌套函数可以访问外部函数中的局部变量。 In C++11, you can define a lambda function, with automatic capture. 在C ++ 11中,您可以使用自动捕获定义lambda函数。

The reason C and C++ never adopted nested functions is because in order to make capture work, you need additional information, with the result that a pointer to function becomes more complex. C和C ++从未采用嵌套函数的原因因为为了使捕获工作,您需要额外的信息,结果是指向函数的指针变得更加复杂。 With the result that either you cannot take the address of the nested function (lack of orthogonality), a pointer to a nested function is incompatible with a normal pointer to a function (which ends up requiring too many external details to perculate out), or all pointers to functions have the extra information (and you pay for something you don't use most of the time). 结果是你不能获取嵌套函数的地址(缺乏正交性),指向嵌套函数的指针与指向函数的普通指针不兼容(最终需要太多的外部细节来实现),或者所有指向函数的指针都有额外的信息(你需要为大部分时间都不用的东西付费)。

Nested functions are only allowed in C but are seldomly used because they are visible within that function scope only. 嵌套函数仅在C中允许但很少使用,因为它们仅在该函数范围内可见。 However if you want to workaround nested functions you can do something like this 但是,如果要解决嵌套函数,可以执行以下操作

#include<stdio.h>
typedef void (*fptr)(void);
fptr test1(void) {
    void test2(void) {
        printf("test2\n");
    }
printf("test1\n");
return test2;
}
int main(void) {
    void (*f)(void);
    f = test1();
    f();
    return 0;
 }

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

相关问题 为什么我不能在另一个函数中定义一个函数? - Why can't I define a function inside another function? 为什么我不能在另一个文件中定义内联成员函数? - Why I can't define inline member function in another file? 如果要从另一个翻译单元链接某个成员函数,为什么不能在该类中定义该成员函数? - Why can I not define a member function in a class if that function is to be linked from another translation unit? 为什么我不能在主 function 之外定义一个类的 object(继承了另一个类)? - Why can't I define a object of class(which inherited another class) outside the main function? "为什么我可以在 C++ 中的函数中定义结构和类?" - Why can I define structures and classes within a function in C++? 为什么iostream定义了abs函数,我该如何阻止它呢? - Why does iostream define an abs function, and how can I stop it? 为什么不能使用函数的 typedef 来定义函数? - Why can't a typedef of a function be used to define a function? 我可以在函数范围中使用define指令吗? - Can I use define directive in a function scope? 我在哪里可以为私人功能定义身体? - Where can I define the body for a private function? 为什么我不能将此成员函数作为另一个类的朋友? - Why can't I make this member function friend of another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM