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() {}
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.
3. Which is the correct syntax of declaring a namespace? namespace B{ int i; };
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 .
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With