Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

namespaces having unnamed namespace with the same variable declared

Tags:

c++

namespaces

I tried this dummy code below to test unnamed namespace.

I have the following output

 ctor 1
 ctor 0
 3
 5

I am a bit confused about this.

  1. I was expecting an error from the compiler saying that it cannot resolve an ambiguity regarding a::m_a. Instead it refers always to the less nested. Is it always the case? What rules C++ is following?
  2. It seems that the compiler creates variable CMyObj following the order written on the file. Is this always the case?
  3. is there any way to access the most nested m_a variable from main()?.
class CMyObj{     
    public:
    CMyObj(int a){std::cout  << "ctor " << a << std::endl; }
 };
 namespace a{ 
      namespace{
           int m_a=4;
           int m_b=5;
           CMyObj m_obj(1);
      }  
 }
 namespace a{
      int m_a=3;
      CMyObj m_obj(0);
 }
 int main(){
      std::cout << a::m_a << std::endl; // which one?
      std::cout << a::m_b << std::endl; // how this is possible?
      return 0;
 }
like image 653
Abruzzo Forte e Gentile Avatar asked Dec 01 '11 23:12

Abruzzo Forte e Gentile


People also ask

What is an unnamed namespace?

Unnamed NamespacesThey are directly usable in the same program and are used for declaring unique identifiers. In unnamed namespaces, name of the namespace in not mentioned in the declaration of namespace. The name of the namespace is uniquely generated by the compiler.

What is the purpose of anonymous namespace in C++?

An anonymous namespace makes the enclosed variables, functions, classes, etc. available only inside that file. In your example it's a way to avoid global variables. There is no runtime or compile time performance difference.

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).

Can you use two namespaces C++?

You can have the same name defined in two different namespaces, but if that is true, then you can only use one of those namespaces at a time. However, this does not mean you cannot use the two namespace in the same program. You can use them each at different times in the same program.


1 Answers

I don't have C++03 standard with me to check the wording there, so I will quote from FDIS n3290. I think the answer to this question is found in qualified name lookup rules in 3.4.3.2/2:

For a namespace X and name m, the namespace-qualified lookup set S(X,m) is defined as follows: Let S0(X,m) be the set of all declarations of m in X and the inline namespace set of X (7.3.1). If S0(X,m) is not empty, S(X,m) is S0(X,m); otherwise, S(X,m) is the union of S(Ni,m) for all namespaces Ni nominated by using-directives in X and its inline namespace set.

Now, remember that unnamed namespace is a uniquely named namespace with a using directive.

like image 113
Gene Bushuyev Avatar answered Sep 25 '22 17:09

Gene Bushuyev