C ++ - function declarations inside function areas?

Some time ago I went through a draft of the C ++ 11 standard and came across this one (in ยง8.3.6, p. 204):

void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow // a parameter with a default argument void f(int, int); void f(int, int = 7); void h() { f(3); // OK, calls f(3, 7) void f(int = 1, int); // error: does not use default // from surrounding scope } void m() { void f(int, int); // has no defaults f(4); // error: wrong number of arguments void f(int, int = 5); // OK f(4); // OK, calls f(4, 5); void f(int, int = 5); // error: cannot redefine, even to // same value } void n() { f(6); // OK, calls f(6, 7) } 

This was due to the default settings for functions. I was struck by the fact that function declarations appeared in the scope of a function. Why? What is this feature used for?

+6
source share
2 answers

Although I had no idea you could do this, I tested it and it works. I think you can use it for the forward-declare functions defined below:

 #include <iostream> void f() { void g(); // forward declaration g(); } void g() { std::cout << "Hurray!" << std::endl; } int main() { f(); } 

If you delete the declaration forward, the program will not compile. That way, you can have visibility based on scope based on visibility.

+6
source

Any function / variable declaration has visibility and scope. For example, if only class members can see in a class. If in a function only a function can be visible, after the declaration of a variable or function.

Usually we use data structures within the scope. But the compiler grammar rule applies to both, since the function itself has an address and, therefore, visibility applies to it as well.

0
source

Source: https://habr.com/ru/post/982361/


All Articles