Question: 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 regions of `T`, `U` and `V` are the *template-declaration*s on
>  lines `#1`, `#2` and `#3`, respectively. But the names `A`, `f`, `g` and `C` all
>  belong to the same declarative region — namely, the *namespace-body* of
>  `N`.
> 
> 
> 

It's not clear to me why `N`s body is the declarative region of `g` and `C`. I thought it was class `B`s body.  

Can someone clarify what the standard means by that?

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

> 
> for an *elaborated-type-specifier* of the form
> 
> 
>          *class-key
>  identifier*
> 
> 
> if the *elaborated-type-specifier* is used in the *decl-specifier-seq* or
>  *parameter-declaration-clause* of a function defined in namespace scope, […]; **otherwise**, except as a friend declaration, **the identifier is declared in the smallest namespace or block scope that contains the declaration.**
> 
> 
> 

(The "otherwise …" part solely applies to declarations of the form `friend class C;`; it uses "as" and not "inside")  

Thus as `C` is declared as a member of `N`, clearly its declarative region is `N`'s body. In fact, you [can use `C` outside `B`](http://coliru.stacked-crooked.com/a/099308b5ba9fe3d7).

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 enclosing
>  namespace.**
> 
> 
> 

Thus the declarative region of `g` is also `N`'s body.
Because `g` is a friend function it's scope is the namespace scope, as per section `11.3` *Friends*:

> 
> A function can be defined in a friend declaration of a class if and
>  only if the class is a non-local class (9.8), the function name is
>  unqualified, and the function has 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 can see this also applies to function templates from section `7.3.1.2`:

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

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

> 
> The point of declaration of a class first declared in an
>  elaborated-type-specifier is as follows:
> 
> 
> 

and includes the following bullet (*emphasis mine*):

> 
> * for an elaborated-type-specifier of the form
> 
> 
> class-key identifier
> 
> 
> if the elaborated-type-specifier is used in the decl-specifier-seq or
>  parameter-declaration-clause of a function defined in namespace scope,
>  the **identifier is declared as a class-name in the namespace that
>  contains the declaration**;
> 
> 
>