How can I "unuse" a namespace?

C++NamespacesC++builder

C++ Problem Overview


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;

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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.

Solution 3 - C++

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

Solution 4 - C++

For future reference : since the XE version there is a new value that you can #define to avoid the dreaded using namespace System; int the include : DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE

Solution 5 - C++

How about using sed, perl or some other command-line tool as part of your build process to modify the generated headers after they are generated but before they are used?

Solution 6 - C++

Quick experiment with Visual Studio 2005 shows that you can enclose those headers in your own named namespace and then use what you need from this namespace (but don't use the whole namespace, as it will introduces the namespace you want to hide.

Solution 7 - C++

#include<iostream>
#include<stdio.h>
namespace namespace1 {
    int t = 10;
}
namespace namespace2 {
    int t = 20;
}
int main() {
using namespace namespace1;
    printf("%d" , t);
    printf("%d" , namespace2::t);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRoddyView Question on Stackoverflow
Solution 1 - C++Head GeekView Answer on Stackoverflow
Solution 2 - C++jk.View Answer on Stackoverflow
Solution 3 - C++EclipseView Answer on Stackoverflow
Solution 4 - C++cdelacroixView Answer on Stackoverflow
Solution 5 - C++user3458View Answer on Stackoverflow
Solution 6 - C++KasprzolView Answer on Stackoverflow
Solution 7 - C++Narendra kumawatView Answer on Stackoverflow