c++ - 在 C++11 标准的 §7.3.1.2/3 中有一些我不理解的细节

标签 c++ c++11 namespaces language-lawyer friend

C++11 标准中的§7.3.1.2/3(重点是我的):

Every name first declared in a namespace is a member of that namespace. If a friend declaration in a nonlocal class first declares a class or function the friend class or function is a member of the innermost enclosing namespace. The name of the friend is not found by unqualified lookup (3.4.1) or by qualified lookup (3.4.3) until a matching declaration is provided in that namespace scope (either before or after the class definition granting friendship). If a friend function is called, its name may be found by the name lookup that considers functions from namespaces and classes associated with the types of the function arguments (3.4.2). If the name in a friend declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier, the lookup to determine whether the entity has been previously declared shall not consider any scopes outside the innermost enclosing namespace. [ Note: The other forms of friend declarations cannot declare a new member of the innermost enclosing namespace and thus follow the usual lookup rules.

示例:

// Assume f and g have not yet been defined.
void h(int);
template <class T> void f2(T);
namespace A {
    class X {
        friend void f(X); // A::f(X) is a friend
        class Y {
            friend void g(); // A::g is a friend
            friend void h(int); // A::h is a friend
            // ::h not considered
           friend void f2<>(int); // ::f2<>(int) is a friend
        };
    };
    // A::f, A::g and A::h are not visible here
    X x;
    void g() { f(x); } // definition of A::g
    void f(X) { /* ... */} // definition of A::f
    void h(int) { /* ... */ } // definition of A::h
    // A::f, A::g and A::h are visible here and known to be friends
}
using A::x;
void h() {
    A::f(x);
    A::X::f(x); // error: f is not a member of A::X
    A::X::Y::g(); // error: g is not a member of A::X::Y
}

除非我遗漏了什么,否则我不明白上面的词 first 的必要性。据我所知,命名空间中任何实体的声明不能超过一个,类中友元函数的声明也不能超过一个。

此外,示例中注释“假设 f 和 g 尚未定义”的相关性是什么?这些函数是否在命名空间 A 的定义之前声明并不重要。它们必然属于全局命名空间,并且与命名空间 A 内声明的函数无关。

编辑:

一个人可以重复声明同一个函数,或者在一个命名空间中声明和定义一个函数,这一事实并没有使我的观察无效,即在§7.3.1.2/3 不是必需的。

编辑1

我刚刚发现另一个错误。评论::f2<>(int) is a friend是不正确的。不但没有定义模板函数f2(T)在命名空间 A 中,但更重要的是声明 template <class T> void f2(T);必须在内部 A,否则函数f2<>(int)不会是类友A::X::Y .

最佳答案

希望是比 Vlad 的完整答案更短更简洁的答案:

一个实体可以声明多次,你的前提是错误的。在第一句中,first 很重要,因为这两个是命名空间 N 中函数 f 的有效声明:

namespace N { void f(); }
void N::f() { ... }        // A definition is *also* a declaration

此时第一句中first的需求就很明显了,fN命名空间的成员(第一个 声明),而不是全局命名空间。

在 friend 声明的情况下,first 出于不同的原因很重要,就好像 friend 声明是 first 声明一样,名称对于常规来说是不可见的查找:

//[1]
class Y {};                       // Some type
class X {
   X(Y);                          // allow implicit conversions, 
                                  //    for exposition purposes
   friend X operator+(X, X) {...} // *first* declaration of this operator
};                                //    and also the definition
void f() {
   Y a, b;
   a + b;                         // error, no operator+ takes two Y
   X c; 
   c + b;                         // OK, ADL can find it
}

如果友元声明不是第一个声明,即如果 [1] 被之前的声明替换:

class X;
X operator+(X,X);

在所有其余代码相同的情况下,上面的代码将编译并调用 operator+(X,X) 转换 abX

你的最后一个问题是关于假设 fg 没有被定义,我认为应该读作 declared ,未定义。这个声明的重要性在于,如果函数已经在手边声明,那么注释//A::f, A::g and A::h 在这里是不可见的 变为 false,因为前面的声明使这些函数可见。

关于c++ - 在 C++11 标准的 §7.3.1.2/3 中有一些我不理解的细节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23082525/

相关文章:

c++ - dynamic_cast 和右值引用

c++ - 在此范围内未声明“敌人”?

c++11 - 模板元编程 - 错误 : template parameters not used in partial specialization

c++ - this指针是如何捕获的?

ruby - 如何将模块命名空间添加到 ruby​​ 类/模块?

c++ - 如何解决此 lint 警告 "Implicit binary conversion from int to unsigned int"

c++ - 如何在 Eclipse CDT 中启用 C++11/C++0x 支持?

python - 在 python 中,有没有办法从对象本身找到包含变量或其他对象的模块?

C++ 编译器选择了错误的命名空间

c++ - 错误 C2259 : "Derived" cannot instantiate abstract class