If you load all elements from both namespace to current scope by use of using namespace
directove:
using namespace FirstLib;
using namespace SecondLib;
and there is potential that some of the names in those namespace may clash, then you need to tell compiler explicitly which of the element you want to use, in current scope, by use of using
declaration:
using SecondLib::Foobar;
As the C++ standard says:
7.3.3 The using declaration
1 A using-declaration introduces a
name into the declarative region in
which the using-declaration appears.
That name is a synonym for the name of
some entity declared elsewhere.
This line requests compiler to think SecondLib::Foobar
whenever it sees Foobar
for the rest of current scope in which the using declaration was used.
The using
directive and declaration is very helpful, but they may cause problems as the one you're dealing with. So, it's a good idea to narrow use of any form of using
to minimal scope possible. You can use the directive using namespace
in scope of other namespace
namespace MyApp {
using namespace ::SecondLib;
}
or even a function. The same applies to using
declaration. So, it's a good idea to narrow the scope of use of any of them:
void foo()
{
::SecondLib::Foobar fb;
}
It may seem tedious, but it is only when you type, though you most likely use intellisense-enabled editor, so cost is really small, but benefits are large - no confusions, readable code, no compilation issues.
It's also a very good idea to NOT to use using namespace
in header scope. See Header files usage - Best practices - C++
My own tip: do use full qualification whenever you need to use any of these types