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;
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.
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.
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; .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With