Can we have functions inside functions in C++?

后端 未结 12 2158
抹茶落季
抹茶落季 2020-11-22 14:51

I mean something like:

int main() 
{
  void a() 
  {
      // code
  }
  a();

  return 0;
}
相关标签:
12条回答
  • 2020-11-22 15:22

    No, it's not allowed. Neither C nor C++ support this feature by default, however TonyK points out (in the comments) that there are extensions to the GNU C compiler that enable this behavior in C.

    0 讨论(0)
  • 2020-11-22 15:26

    But we can declare a function inside main():

    int main()
    {
        void a();
    }
    

    Although the syntax is correct, sometimes it can lead to the "Most vexing parse":

    #include <iostream>
    
    
    struct U
    {
        U() : val(0) {}
        U(int val) : val(val) {}
    
        int val;
    };
    
    struct V
    {
        V(U a, U b)
        {
            std::cout << "V(" << a.val << ", " << b.val << ");\n";
        }
        ~V()
        {
            std::cout << "~V();\n";
        }
    };
    
    int main()
    {
        int five = 5;
        V v(U(five), U());
    }
    

    => no program output.

    (Only Clang warning after compilation).

    C++'s most vexing parse again

    0 讨论(0)
  • 2020-11-22 15:28

    Let me post a solution here for C++03 that I consider the cleanest possible.*

    #define DECLARE_LAMBDA(NAME, RETURN_TYPE, FUNCTION) \
        struct { RETURN_TYPE operator () FUNCTION } NAME;
    
    ...
    
    int main(){
      DECLARE_LAMBDA(demoLambda, void, (){ cout<<"I'm a lambda!"<<endl; });
      demoLambda();
    
      DECLARE_LAMBDA(plus, int, (int i, int j){
        return i+j;
      });
      cout << "plus(1,2)=" << plus(1,2) << endl;
      return 0;
    }
    

    (*) in the C++ world using macros is never considered clean.

    0 讨论(0)
  • 2020-11-22 15:30

    No.

    What are you trying to do?

    workaround:

    int main(void)
    {
      struct foo
      {
        void operator()() { int a = 1; }
      };
    
      foo b;
      b(); // call the operator()
    
    }
    
    0 讨论(0)
  • 2020-11-22 15:33

    Local classes have already been mentioned, but here is a way to let them appear even more as local functions, using an operator() overload and an anonymous class:

    int main() {
        struct {
            unsigned int operator() (unsigned int val) const {
                return val<=1 ? 1 : val*(*this)(val-1);
            }
        } fac;
    
        std::cout << fac(5) << '\n';
    }
    

    I don't advise on using this, it's just a funny trick (can do, but imho shouldn't).


    2014 Update:

    With the rise of C++11 a while back, you can now have local functions whose syntax is a little reminiscient of JavaScript:

    auto fac = [] (unsigned int val) {
        return val*42;
    };
    
    0 讨论(0)
  • 2020-11-22 15:33

    Starting with C++ 11 you can use proper lambdas. See the other answers for more details.


    Old answer: You can, sort-of, but you have to cheat and use a dummy class:

    void moo()
    {
        class dummy
        {
        public:
             static void a() { printf("I'm in a!\n"); }
        };
    
        dummy::a();
        dummy::a();
    }
    
    0 讨论(0)
提交回复
热议问题