96

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;
3
  • 3
    You should probably open a bug report with their QC system: qc.codegear.com Commented Oct 3, 2008 at 18:23
  • 2
    BTW, which auto-generated headers are these? Commented Oct 17, 2008 at 16:36
  • 2
    One day C++ will have modules, and including code into other code will have better encapsulation constructs. Until then, there is not an easy way around this. Consider putting your own code into a namespace and referring to it that way. Commented Jun 30, 2015 at 2:29

7 Answers 7

66

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.

8
  • 1
    In general, this is a terrible idea. C++ headers are not intended to be included in an alternately namespace as was used here.
    – Aaron
    Commented Oct 3, 2008 at 18:06
  • 29
    It's a terrible idea to include a using directive in a header file too. This simply mitigates that problem.
    – Head Geek
    Commented Oct 3, 2008 at 18:16
  • 4
    Placing the header in your own namespace is not a solution as it changes the meaning of the declarations in that library. (-1) Commented Oct 5, 2008 at 14:57
  • 4
    That depends entirely on what's being declared in the header.
    – Head Geek
    Commented Oct 8, 2008 at 15:31
  • 1
    Which is precisely why it is undefined behavior. Commented Oct 17, 2008 at 16:36
64

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.

6
  • Can you wrap an include in a block like this though?
    – Eclipse
    Commented Oct 3, 2008 at 17:43
  • Yes this won't with auto generated code. Byt mybe he can change the template for the auto generated code?
    – jk.
    Commented Oct 3, 2008 at 17:53
  • Yeah this doesn't really address the problem he's having of headers using namespaces.
    – Kip
    Commented Oct 3, 2008 at 17:57
  • Unfortunately this is not true. Try this:
    – Adam
    Commented Oct 21, 2010 at 11:52
  • namespace xyzzy{ const int i{ using namespace xyzzy; } // stop using namespace xyzzy here
    – Adam
    Commented Oct 21, 2010 at 11:53
18

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
0
10

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

1
  • But this seems not to work properly. At least in all cases I tried (with BCB6). I then used to fall back on adding explicit namespaces on conflict and - even worse - include a header for avoiding type name conflicts...
    – Wolf
    Commented Sep 2, 2014 at 8:15
3

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?

0
1

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.

1
  • 1
    This will likely cause name-mangling issues if the header files are declarations for a library. The compile will succeed, but the linker won't be able to find the definitions, as they would have already been compiled in a different namespace.
    – Eclipse
    Commented Oct 3, 2008 at 17:42
1
#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);
}
3
  • 1
    please explain your answer!
    – Mazz
    Commented Apr 26, 2017 at 9:39
  • you can use scope resolution operator to use another namespace variable Commented Apr 26, 2017 at 12:29
  • 3
    Does not address the question
    – M.M
    Commented May 2, 2017 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.