Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace search order

Tags:

c++

I have two namespaces that each have a function with the same name. If from one of the namespaces I want to call the function that matches the best. From a function in NamespaceA, if I call MyFunction(...), of course it uses the one in NamespaceA. However, if I add a 'using NamespaceB::MyFunction', I would then expect the behavior I described. However, what I actually see is that it ALWAYS finds the NamespaceB function, even though I am in NamespaceA. HOWEVER, if I ALSO add a using::NamespaceA (even though I am already in NamespaceA), it works as I'd expect. A demonstration is below. Can anyone explain how this works?

#include <iostream>

namespace NamespaceA
{
  void DoSomething();
  void MyFunction(int object);
}

namespace NamespaceB
{
  void MyFunction(float object);
}

namespace NamespaceA
{
  void DoSomething()
  {
    using NamespaceA::MyFunction; // Note that without this line the lookup always fins the NamespaceB::MyFunction!
    using NamespaceB::MyFunction;

    MyFunction(1);
    MyFunction(2.0f);
  }

  void MyFunction(int object)
  {
    std::cout << "int: " << object << std::endl;
  }
}

namespace NamespaceB
{
  void MyFunction(float object)
  {
    std::cout << "float: " << object << std::endl;
  }
}

int main(int argc, char *argv[])
{
  NamespaceA::DoSomething();

  return 0;
}
like image 363
David Doria Avatar asked Feb 21 '12 16:02

David Doria


People also ask

How do namespaces work in C++?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

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.

What is a namespace example?

Prominent examples for namespaces include file systems, which assign names to files. Some programming languages organize their variables and subroutines in namespaces. Computer networks and distributed systems assign names to resources, such as computers, printers, websites, and remote files.

What is global namespace in C++?

Beginning of C++ only. Global scope or global namespace scope is the outermost namespace scope of a program, in which objects, functions, types and templates can be defined. A name has global namespace scope if the identifier's declaration appears outside of all blocks, namespaces, and classes.


1 Answers

It has to do with the order in which different parts of the program are looked in to find a name. For the situation you mention, it has to do with the scope of the function's top-level block being searched for before the enclosing namespace. Basically, the using declaration brings that name into the top-level scope of DoSomething, and since that scope is looked in before the enclosing namespace scope, then if a matching function is found there, then the enclosing namespace scope isn't considered.

I have glossed over a lot of stuff that isn't relevant in your example (for example, if the argument were not a built-in type then, believe it or not, names from the scope where that type was defined could be considered as well. For the whole story, see section 3.4 here. It's pretty scary, about 13 pages to describe all this stuff; but don't bother with it unless you really are curious, because most of the stuff is there so that it "works the way you expect", more-or-less. That document is not the real Standard but actually a working draft with some corrections, so it is basically the real C++ Standard plus some bugfixes.

like image 138
chisophugis Avatar answered Oct 21 '22 03:10

chisophugis