1

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?

Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • "using One::foo;" is called a using-declaration; a using-directive is "using namespace N;". –  Nov 06 '10 at 19:39

2 Answers2

1

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.

0

As for the usage they are equivalent. As for code, in case a) you're duplicating the function foo() code. That is, both versions will make available the foo() function inside Two, but the a) case generates the code for foo twice, because the compiler doesn't have any hint in discovering it is the same function.

Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87