Yes, it is possible:
void foo()
{
using namespace abc;
....
}
or
void foo()
{
using abc::x;
using abc::y;
using abc::z;
....
}
Question
I have to define two functions, say, foo()
and bar()
in the same namespace
and the same file. For the definition of the first, foo()
, I want to use all symbols of, say, namespace other
, but don't want symbols from namespace other
to be automatically in scope for my other function, bar()
. Is this possible? How?
(note: I don't want to know about alternative "solutions" either avoiding this problem of mitigating it, such as namespace o=other
etc.)
Solution
Yes, it is possible:
void foo()
{
using namespace abc;
....
}
or
void foo()
{
using abc::x;
using abc::y;
using abc::z;
....
}
OTHER TIPS
#include <iostream>
void quux() { std::cout << "root\n"; }
namespace other {
void quux(int x = 0) { std::cout << "other\n"; }
}
namespace taxes {
void foo() {
using namespace other;
quux(3);
};
void bar() {
quux();
}
}
int main() {
taxes::foo();
taxes::bar();
}
Note that quux
in bar
would be ambiguous if it could see other::quux
, but it cannot.
On the other hand, this does not give you access to namespace other
in the 'head' of the function foo
(the parameters etc), but that is a rare requirement. There might be a solution involving inline namespace
s or the like, but probably not worth the confusion.