C++ - Function declarations inside function scopes?

*爱你&永不变心* 提交于 2019-12-05 06:05:23

Although I had no idea you can do this, I tested it and it works. I guess you may use it to forward-declare functions defined later, like below:

#include <iostream>

void f()
{
    void g(); // forward declaration
    g();
}

void g()
{
    std::cout << "Hurray!" << std::endl;
}

int main()
{
    f();
}

If you remove the forward declaration, the program won't compile. So in this way you can have some kind of scope-based forward declaration visibility.

Any function/variable declaration has its visibility and scope. For example, if in class, only class members can see to it. If in function only the function can have visibility to it, after we declare the variable or function.

We generally use data structures within function scope. But compiler's grammar rule is applicable to both, as function in itself has address and hence visibility is applicable to it as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!