Search code examples
c++namespaceslanguage-lawyerdefinitionusing-declaration

Trying to define namespace member via using-declaration


Consider the following program. Is it well-formed or not according to the c++ standard (references to relevant parts of the standard needed):

namespace X { extern int i; }

namespace N { using X::i; }

int N::i = 1;

int main() {}

I'm getting different results for different compilers. I'm trying to figure out for what compiler I should file a bug report for:

  • Clang: Gives the following compiler error: No member named 'i' in namespace 'N'

  • GCC and Visual C++ compiles it without errors.

For comparison the following gives compiler error with all three compilers:

namespace X { void f(); }

namespace N { using X::f; }

void N::f() {};

int main() {}

Solution

  • Current working draft N4527, [8.3p1]:

    [...] 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; the member shall not merely have been introduced by a using-declaration in the scope of the class or namespace nominated by the nested-name-specifier of the declarator-id. [...]

    So, definitely ill-formed; GCC and MSVC are wrong.