c++ - 函数参数的声明区域

标签 c++ templates language-lawyer

我们有来自 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-declarations 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.

我不清楚为什么 N 的主体是 gC 的声明区域。我以为是 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/

相关文章:

c++ - 此函数或变量可能是不安全的 visual studio

c++ - 链表示例中的指针

c++ - 平板电脑上的手写笔 "lifts"会怎样?

c++ - std::vector 等不必要地(错误地?)实例化嵌套模板参数类型

c++ - 是否可以将模板派生的 C++ 类与 Qt 的 Q_OBJECT 混合使用?

c++ - 使用标准库构建/解析 UTC 日期

javascript - 如何使用 Node.js、EJS 模板和动态文件名进行多重渲染

c - a[][] 和 (*a)[] 不等同于函数参数吗?

c++ - 为什么编译代码 "foo::foo::foo::foob"?

c - 位字段如何与 C 中的位填充相互作用