Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() {}
like image 931
Supremum Avatar asked Jul 18 '15 22:07

Supremum


People also ask

How do you declare namespace?

Declaring namespaces and namespace membersTypically, you declare a namespace in a header file. If your function implementations are in a separate file, then qualify the function names, as in this example. A namespace can be declared in multiple blocks in a single file, and in multiple files.

Which is the correct method for declaring a namespace?

3. Which is the correct syntax of declaring a namespace? namespace B{ int i; };

What is the use of using declaration in C++?

A using declaration in a definition of a class A allows you to introduce a name of a data member or member function from a base class of A into the scope of A .

What are namespaces and how they are declared?

A Namespace is a set of unique names. Namespace is a mechanisms by which element and attribute name can be assigned to a group. The Namespace is identified by URI(Uniform Resource Identifiers).


1 Answers

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.

like image 61
bogdan Avatar answered Oct 11 '22 04:10

bogdan