Declarative function parameter domain

We have the following example from N4296::3.3.9/2 [basic.scope.temp] :

 namespace N { template<class T> struct A { }; // #1 template<class U> void f(U) { } // #2 struct B { template<class V> friend int g(struct C*); // #3 }; } 

The declarative areas T , U and V are the template announcements on lines #1 , #2 and #3 respectively. But the names A , f , g and C all refer to the same declarative region, namely, the namespace N

It is not clear to me why the body N is a declarative region of g and C I thought it was class B
Can someone clarify what the standard means?

+6
source share
2 answers

C first declared in g , so [basic.scope.pdecl] / (7.2) applies

for the specified form specifier type

key class Identifier

if the specified type specifier is used in the qualifier-description-seq or parameter-declaration-sentence of the function defined in the namespace area, [...]; otherwise , except for declaring a friend, the identifier is declared in the smallest namespace or block area containing the declaration.

(The "otherwise ..." part only applies to declarations of the form friend class C; it uses "how" and not "inside")
Thus, as C declared as a member of N , its declarative scope is clearly N In fact, you can use C outside of B

And g is a member of N according to [namespace.memdef] / 3

If a friend declaration in a non-local class first declares [..] a function template, the friend is a member of the innermost namespace environment.

Thus, the declarative region g also the body of N

+4
source

Since g is a friend function, scope is the namespace scope, according to Section 11.3 Friends:

A function can be defined in the declaration of a friend of the if class and only if the class is a nonlocal class (9.8), the name of the function is unqualified, and the function has a namespace scope. [Example:

 class M { friend void f() { } // definition of global f, a friend of M, // not the definition of a member function }; 

-end example]

We see that this also applies to the function templates from section 7.3.1.2 :

If declaring a friend in a non-local class first declares the class, the function, template of the template, or template of function 97 of the friend is a member of the innermost enclosing namespace. [...]

and for C this is described in section 3.3.2 Point of declaration:

The declaration point of the class first declared in the specified type specifier is as follows:

and includes the following brand (highlighted by me):

  • for the specified form specifier type

    class id

if the specified type specifier is used in the qualifier-description-seq or parameter-declaration-sentence of the function defined in the namespace area, the identifier is declared as the class name in the namespace, contains the declaration ;

+2
source

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


All Articles