Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; 
like image 473
Roddy Avatar asked Oct 03 '08 16:10

Roddy


People also ask

How do you Unuse namespace in 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. Maybe you can change the template which is used of your auto-generated headers.

How do you define a namespace?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

How do I stop using namespace?

By using using namespace std you defy the whole idea of namespaces, by polluting top one with unnecessary entries. If you frequently use some namespaced elements (like std::cout , std::endl ), pull just them with using std::cout; using std::endl; or even in the newest version of C++ with using std::cout, std::endl; .

What is an example namespace?

In an operating system, an example of namespace is a directory. Each name in a directory uniquely identifies one file or subdirectory. As a rule, names in a namespace cannot have more than one meaning; that is, different meanings cannot share the same name in the same namespace.


1 Answers

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.

like image 188
Head Geek Avatar answered Oct 06 '22 11:10

Head Geek