Search code examples
c++namespacesusing-declaration

Using-declaration to move name to another namespace?


Given:

namespace One {
  void foo(int x) {
    munch(x + 1);
  }
};

namespace Two {
  // ... see later
}

...
void somewhere() {
  using namespace Two;
  foo(42);
  ...

is there any difference between the following two variants:

a)

namespace Two {
  void foo(int x) {
    munch(x + 1);
  }
};

and b)

namespace Two {
  using One::foo;
};

EDIT: It's pretty clear that (a) duplicates the code which should never be a good idea. The question is more about overload resolution etc. ... what if there are other foos or munches in possibly other namespaces?


Solution

  • With a, they are actually different functions, but with b, the two functions are identical:

    assert(&One::foo == &Two::foo);
    

    This would rarely matter; a bigger concern is duplicating the logic.