Re-declaration in a namespace that does not include the initial declaration

A namespace member can be defined in a namespace that spans an ad namespace:

Members of a named namespace can also be defined outside this namespace by explicit qualification (3.4.3.2) of the designated name, provided that the defined object has already been declared in the namespace and the definition appears after the declaration point in the namespace that spans the namespace declarations .

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

A class can be defined in a namespace that spans the declaration namespace:

If the head-class name contains a nested name specifier, the class specifier must refer to a class that was previously declared directly in the class or namespace to which the nested name specifier belongs, or to an element of the built-in namespace (7.3.1) of this namespace ( i.e., not just inherited or introduced use-declarations), but the class specifier should appear in the namespace containing the previous declaration . In such cases, the nested class qualifier of the definition-head-name class should not begin with the decltype specifier.

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

We also have the same rule for defining member function and defining static data.

So my question is, is overriding (not defining) legal in a namespace that does not include the original declaration?

+5
source share
1 answer

Regarding struct ::A; [dcl.type.elab] / 1 your declaration is poorly formed:

If the specified type specifier is the only component of the declaration, the declaration is poorly formed, unless specialization is explicit (14.7.3), explicit instantiation (14.7.2), or it takes one of the following forms :

& emsp; & emsp; class-key attribute-specifier-seq opt & ensp; identifier ;
& emsp; & emsp; friend class-key :: opt identifier ;
& emsp; & emsp; friend class-key :: opt simple-template-id ;
& emsp; & emsp; friend identifier of the identifier of the enclosed name of the key class ;
& emsp; & emsp; friend key-key-nested qualifier template opt simple-template-id ;

I do not see a problem in the case of a function; [dcl.meaning] / 1 allows you to:

When the declarator-id is qualified, the declaration must refer to the previously declared member of the class or namespace to which the qualifier belongs (or, in the case of the namespace, an element of the built-in namespace of this namespace (7.3.1)) or its specialization; [...] [Note: if the qualifier is a global scope operator :: , the identifier identifier refers to the declared name in the global namespace. - final note]

However, both GCC and Clang insist that overrides, like definitions, must occur in the host namespace.

+1
source

All Articles