我们有来自 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
andV
are the template-declarations on lines#1
,#2
and#3
, respectively. But the namesA
,f
,g
andC
all belong to the same declarative region — namely, the namespace-body ofN
.
我不清楚为什么 N
的主体是 g
和 C
的声明区域。我以为是 B
类的尸体。
有人可以阐明标准的含义吗?
最佳答案
C
首先在 g
中声明,因此 [basic.scope.pdecl]/(7.2) 适用
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.
(“否则……”部分仅适用于 friend class C;
形式的声明;它使用“as”而不是“inside”)
因此,当 C
被声明为 N
的成员时,显然它的声明区域是 N
的主体。其实你can use C
outside B
.
并且 g
根据 [namespace.memdef]/3
N
的成员
If a friend declaration in a non-local class first declares a [..] function template the friend is a member of the innermost enclosing namespace.
因此 g
的声明区域也是 N
的主体。
关于c++ - 函数参数的声明区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28241874/