views:

1656

answers:

5

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;
+6  A: 

No you can't unuse an 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.

jk
Can you wrap an include in a block like this though?
Eclipse
Yes this won't with auto generated code. Byt mybe he can change the template for the auto generated code?
jk
Yeah this doesn't really address the problem he's having of headers using namespaces.
Kip
Unfortunately this is not true. Try this:
Adam
namespace xyzzy{ const int i{ using namespace xyzzy;} // stop using namespace xyzzy here
Adam
errm having some serious input problems here. And my statement was wrong anyway. Sorry.
Adam
+1  A: 

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
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
+4  A: 

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; // the stl string class from the std namespace
Eclipse
+9  A: 

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
In general, this is a _terrible_ idea. C++ headers are not intended to be included in an alternately namespace as was used here.
Aaron
It's a terrible idea to include a using directive in a header file too. This simply mitigates that problem.
Head Geek
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
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
"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
That depends entirely on what's being declared in the header.
Head Geek
Which is precisely why it is undefined behavior.
Kris Kumler
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
+5  A: 

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?

Arkadiy
In case there are many headers, this might be too slow
doc
sed slower that code generator? Hard to believe...
Arkadiy