c++ - 在不包含原始声明的命名空间中重新声明

标签 c++ namespaces language-lawyer definition redeclaration

命名空间成员可以在包含声明命名空间的命名空间中定义:

Members of a named namespace can also be defined outside that namespace by explicit qualification (3.4.3.2) of the name being defined, provided that the entity being defined was already declared in the namespace and the definition appears after the point of declaration in a namespace that encloses the declaration’s namespace.

void f();

namespace N { void ::f() {} }       // illegal for definition

namespace N { void ::f(); }         // what about redeclaration?

类可以在包含声明命名空间的命名空间中定义:

If a class-head-name contains a nested-name-specifier, the class-specifier shall refer to a class that was previously declared directly in the class or namespace to which the nested-name-specifier refers, or in an element of the inline namespace set (7.3.1) of that namespace (i.e., not merely inherited or introduced by a using-declaration), and the class-specifier shall appear in a namespace enclosing the previous declaration. In such cases, the nested-name-specifier of the class-head-name of the definition shall not begin with a decltype-specifier.

struct A;

namespace N { struct ::A {}; }      // illegal for definition

namespace N { struct ::A; }         // what about redeclaration?

对于成员函数定义和静态数据成员定义,我们也有相同的规则。

所以我的问题是重新声明(不是定义)在不包含原始声明的命名空间中是否合法?

最佳答案

关于 struct::A;[dcl.type.elab]/1使你的声明格式错误:

If an elaborated-type-specifier is the sole constituent of a declaration, the declaration is ill-formed unless it is an explicit specialization (14.7.3), an explicit instantiation (14.7.2) or it has one of the following forms:

  class-key attribute-specifier-seqopt  identifier ;
  friend class-key ::opt identifier ;
  friend class-key ::opt simple-template-id ;
  friend class-key nested-name-specifier identifier ;
  friend class-key nested-name-specifier templateopt simple-template-id ;

我没有看到函数案例中的问题; [dcl.meaning]/1确实允许:

When the declarator-id is qualified, the declaration shall refer to a previously declared member of the class or namespace to which the qualifier refers (or, in the case of a namespace, of an element of the inline namespace set of that namespace (7.3.1)) or to a specialization thereof; […] [ Note: If the qualifier is the global :: scope resolution operator, the declarator-id refers to a name declared in the global namespace scope. — end note ]

但是,GCC 和 Clang 都坚持重新声明作为定义必须发生在封闭的命名空间中。

关于c++ - 在不包含原始声明的命名空间中重新声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37897810/

相关文章:

c++ - 将 Boost 库与 mingw 一起使用

c++ - 使用用户输入 C++ 创建 n 个不同的 vector

php - 测试 monlog 包时出现 500 错误

c++ - 函数参数列表中的类声明

c++ - RGB颜色排列

c++ - 对链表打印函数的 undefined reference

.net - VB.NET 项目属性命名空间与导入语句?

c++ - GCC 还是 MSVC,谁是正确的?

c++ - 使用按位运算符和 bool 逻辑的绝对值 abs(x)

css - 在CSS Flexbox中,为什么没有“justify-items”和“justify-self”属性?