c++functionscopedeclarationfunction-declaration

Example of function prototype scope


There is a definition of function prototype scope (3.3.4/1 N3797):

In a function declaration, or in any function declarator except the declarator of a function definition (8.4), names of parameters (if supplied) have function prototype scope, which terminates at the end of the nearest enclosing function declarator.

Can we get an example described that rule?


Solution

  • Here is a simple example

    int a;
    
    void f( int a, int a );
    

    The compiler will issue an error for the second parameter a because its name coincides with the name of the first parameter. That is the compiler will report that there is a redefinition of the name a. The same name is defined twice in the same scope.

    Or another example

    struct A {};
    
    void f( int A, struct A );
    

    The first parameter name hides the structure name so the second parameter is defined using the elaborated name of the structure.