简体   繁体   中英

C++ Function declare in function

Why does this compile?

int main() {
    void f();
}

What is f ? Is it a function or a variable and how can I use it?

You have a function declaration. It's OK to declare functions inside a block scope. It's just not possible to define a function in a block. Declaring a function only locally is perfectly fine, eg when you want to refer to some other, existing function:

int main()
{
    int foo();
    return foo();
}

You just have to link this with some translation unit that defines int foo(); .

(It's probably just not very good practice to do this, since it hides the dependency of your code on the other function in a way that's almost impossible to see without careful reading of the code.)

Note that the local declaration hides other overloads of the function, so argument lookup may be different from if you had declared the function at the global scope. See @David Rodríguez's vastly more detailed answer.

This is an old backwards compatibility feature with C, that allows the declaration of a function inside a scope (function level or a scope inside the function). The effect in C is quite simple, it provides the equivalent of regular function declaration, but the name is only visible inside this scope, so the compiler does not need to care about it outside of the current scope.

In C++ things are a bit more complicated. Having namespaces requires more complex lookup rules, having overloads requires overload resolution. In some earlier version of the standard, it was accepted that a local function declaration should hide any other function declaration by the same name (this is consistent with lookup in general), and disable argument dependent lookup. After all, if the user declared it so close to the use, she must want this function to be called . Some time later there was a push to remove the feature from the language altogether but it was confronted by vendors wanting to support it for existing client codebases.

This affects directly what function overloads are visible, and that in turn affects what gets called. It is dangerous as most people are unaware of the lookup differences and should be avoided. Some examples of what this means:

namespace A {
   struct B {
      operator int();
   };
   void g(B const&);
}
void f(int);
void f(double);
void g(int) {
   void f(double);
   f(1);               // calls f(double)
}
int main() {
   void g(int);
   A::B x;
   g(x);               // calls g(static_cast<int>(x))
}

Inside g(int) , the call f(1) would seem to match ::f(int) , but the local declaration hides that overload and leaves ::f(double) as the only candidate for resolution, and that overload is used after converting the value to a double . Inside main , the local declaration disables argument dependent lookup, and forces the use of ::g(int) , for which the object x is converted to an int .

Without the local function declarations, the code would call f(int) and ::A::g(::A::B const&) respectively, and that is what most developers would expect.

It is perfectly fine to declare a function in a function block. But you will get an error upon defining it inside a function block.

int main()
{
    void func();      //fine
    void func() { }   //error
}

You can't define/create a function inside another function, but you can declare a function inside another function.

int main()
{
    void foo();  //ok

    void foo() {
    }   //error
}

A function-declaration is allowed, a function-definition not.

However, the following is well defined code:

#include <iostream>

int main()
{
    // void function() {}; //  a function-definition is not allowed
    struct Function {
        static void apply() {
            std::cout << "Hello World" << std::endl;
        }
    };

    Function::apply();
} 

f is the identifier(name) of a function whose return type is void, here function is just called in main function to use it, you have to declare and define it here is syntax

void f();       //before main function declaration 
int main()      //main 
{}
void f();       //define here the function f.

And last but not the least you can use(call function to do some useful work) this function if and only if you declare and define it first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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