94

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;
Roddy
  • 66,617
  • 42
  • 165
  • 277
  • 3
    You should probably open a bug report with their QC system: http://qc.codegear.com/ – Kris Kumler Oct 03 '08 at 18:23
  • 2
    BTW, which auto-generated headers are these? – Kris Kumler Oct 17 '08 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. – Trevor Hickey Jun 30 '15 at 02:29

7 Answers7

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.

Head Geek
  • 38,128
  • 22
  • 77
  • 87
  • 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 Oct 03 '08 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 Oct 03 '08 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) – Richard Corden Oct 05 '08 at 14:57
  • That's why I said it might be problematic in some cases. Roddy didn't include any details on the contents of these auto-generated headers included. – Head Geek Oct 05 '08 at 16:30
  • "Problematic" doesn't quite sum up that it will result in undefined behaviour - which if you're lucky may result in link errors. – Richard Corden Oct 06 '08 at 00:08
  • 4
    That depends entirely on what's being declared in the header. – Head Geek Oct 08 '08 at 15:31
  • 1
    Which is precisely why it is undefined behavior. – Kris Kumler Oct 17 '08 at 16:36
  • Since the headers are auto-generated, I presume that any code for them would be as well. Presumably it could be modified to put its functions in the same namespace, with minimal effort on Roddy's part. – Head Geek Oct 17 '08 at 17:15
63

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.

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
jk.
  • 6,388
  • 2
  • 26
  • 21
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
Michael Dorst
  • 8,210
  • 11
  • 44
  • 71
Eclipse
  • 44,851
  • 20
  • 112
  • 171
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

cdelacroix
  • 1,289
  • 14
  • 26
  • 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 Sep 02 '14 at 08: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?

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.

Kasprzol
  • 4,087
  • 22
  • 20
  • 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 Oct 03 '08 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);
}
Pang
  • 9,564
  • 146
  • 81
  • 122