How can I "unuse" a namespace?

One of the vagaries of my development system (Codegear C++Builder) is that some of the auto-generated headers insist on having...

using namespace xyzzy

...statements in them, which impact on my code when I least want or expect it.

Is there a way I can somehow cancel/override a previous "using" statement to avoid this.

Maybe...

unusing namespace xyzzy;

Nope. But there's a potential solution: if you enclose your include directive in a namespace of its own, like this...

namespace codegear {
    #include "codegear_header.h"
} // namespace codegear

...then the effects of any using directives within that header are neutralized.

That might be problematic in some cases. That's why every C++ style guide strongly recommends not putting a "using namespace" directive in a header file.


No you can't unuse a namespace. The only thing you can do is putting the using namespace-statement a block to limit it's scope.

Example:

{
    using namespace xyzzy;

} // stop using namespace xyzzy here

Maybe you can change the template which is used of your auto-generated headers.


You may be stuck using explicit namespaces on conflicts:

string x; // Doesn't work due to conflicting declarations
::string y; // use the class from the global namespace
std::string z; // use the string class from the std namespace