Why can't I define a function inside another function?

后端 未结 11 1859
陌清茗
陌清茗 2020-11-27 16:55

This is not a lambda function question, I know that I can assign a lambda to a variable.

What\'s the point of allowing us to declare, but not define a function insid

相关标签:
11条回答
  • 2020-11-27 17:31

    Actually, there is one use case which is conceivably useful. If you want to make sure that a certain function is called (and your code compiles), no matter what the surrounding code declares, you can open your own block and declare the function prototype in it. (The inspiration is originally from Johannes Schaub, https://stackoverflow.com/a/929902/3150802, via TeKa, https://stackoverflow.com/a/8821992/3150802).

    This may be particularily useful if you have to include headers which you don't control, or if you have a multi-line macro which may be used in unknown code.

    The key is that a local declaration supersedes previous declarations in the innermost enclosing block. While that can introduce subtle bugs (and, I think, is forbidden in C#), it can be used consciously. Consider:

    // somebody's header
    void f();
    
    // your code
    {   int i;
        int f(); // your different f()!
        i = f();
        // ...
    }
    

    Linking may be interesting because chances are the headers belong to a library, but I guess you can adjust the linker arguments so that f() is resolved to your function by the time that library is considered. Or you tell it to ignore duplicate symbols. Or you don't link against the library.

    0 讨论(0)
  • 2020-11-27 17:33

    The first one is a function definition, and it is not allowed. Obvious, wt is the usage of putting a definition of a function inside another function.

    But the other twos are just declarations. Imagine you need to use int two(int bar); function inside the main method. But it is defined below the main() function, so that function declaration inside the function makes you to use that function with declarations.

    The same applies to the third. Class declarations inside the function allows you to use a class inside the function without providing an appropriate header or reference.

    int main()
    {
        // This is legal, but why would I want this?
        int two(int bar);
    
        //Call two
        int x = two(7);
    
        class three {
            int m_iBar;
            public:
                three(int bar):m_iBar(13 + bar) {}
                operator int() {return m_iBar;}
        };
    
        //Use class
        three *threeObj = new three();
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-27 17:37

    Well, the answer is "historical reasons". In C you could have function declarations at block scope, and the C++ designers did not see the benefit in removing that option.

    An example usage would be:

    #include <iostream>
    
    int main()
    {
        int func();
        func();
    }
    
    int func()
    {
        std::cout << "Hello\n";
    }
    

    IMO this is a bad idea because it is easy to make a mistake by providing a declaration that does not match the function's real definition, leading to undefined behaviour which will not be diagnosed by the compiler.

    0 讨论(0)
  • 2020-11-27 17:38

    Specifically answering this question:

    From the answers it seems that there in-code declaration may be able to prevent namespace pollution, what I was hoping to hear though is why the ability to declare functions has been allowed but the ability to define functions has been disallowed.

    Because consider this code:

    int main()
    {
      int foo() {
    
        // Do something
        return 0;
      }
      return 0;
    }
    

    Questions for language designers:

    1. Should foo() be available to other functions?
    2. If so, what should be its name? int main(void)::foo()?
    3. (Note that 2 would not be possible in C, the originator of C++)
    4. If we want a local function, we already have a way - make it a static member of a locally-defined class. So should we add another syntactic method of achieving the same result? Why do that? Wouldn't it increase the maintenance burden of C++ compiler developers?
    5. And so on...
    0 讨论(0)
  • 2020-11-27 17:39

    Nested function declarations are allowed probably for 1. Forward references 2. To be able to declare a pointer to function(s) and pass around other function(s) in a limited scope.

    Nested function definitions are not allowed probably due to issues like 1. Optimization 2. Recursion (enclosing and nested defined function(s)) 3. Re-entrancy 4. Concurrency and other multithread access issues.

    From my limited understanding :)

    0 讨论(0)
提交回复
热议问题