is it possible in C or C++ to create a function inside another?

后端 未结 15 1939
迷失自我
迷失自我 2020-12-15 09:18

Could someone please tell me if this is possible in C or C++?

void fun_a();
//int fun_b();
...
main(){
   ...
   fun_a();
   ...
   int fun_b(){
     ...
            


        
15条回答
  •  时光说笑
    2020-12-15 09:52

    No but in C++0x you can http://en.wikipedia.org/wiki/C%2B%2B0x#Lambda_functions_and_expressions which may take another few years to fully support. The standard is not complete at the time of this writing.

    -edit-

    Yes

    If you can use MSVC 2010. I ran the code below with success

    void test()
    {
     []() { cout << "Hello function\n"; }();
     auto fn = [](int x) -> int { cout << "Hello function (" << x << " :))\n"; return x+1; };
     auto v = fn(2);
     fn(v);
    }
    

    output

    Hello function
    Hello function (2 :))
    Hello function (3 :))
    

    (I wrote >> c:\dev\loc\uniqueName.txt in the project working arguments section and copy pasted this result)

提交回复
热议问题