Re: UN-using a namespace?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"Dave Calkins" <david.calkins@xxxxxxxxxxxxxxx> schrieb im Newsbeitrag
news:uJt1NTLZHHA.4940@xxxxxxxxxxxxxxxxxxxxxxx
I'm familiar with the "using namespace" to expose the members of a
namespace to the global namespace. Is there a way to UN-use a namespace?

We're calling a third party library which, in its headers has "using
namespace" for its own namespace. This ends up polluting the global
namespace and causing conflicts in our code. For example, the library
defines a CPoint class which, of course conflicts with the MFC CPoint
class.

So, I'd like to unuse the namespace after including the third party
library header files to avoid the problem.

Any ideas?

Perhaps you are lucky and the offending headers only use using to inject
their own namespace into the global one. Then you could try something like

#define using /##/
#include "OffendingHeader.h"
#undef using

Not really nice, but using using in a header isn't really nice either. I
don't remember what the standard has to say about /##/, but it works with VS
2005:

~~~~~
// BadHeader.h
namespace foo
{
int x;
}
using namespace foo;
~~~~~
// Test.cpp
#define using /##/
#include "BadHeader.h"
#undef using
double x;
~~~~~

Test.cpp compiles with VS 2005, but fails to compile if #define using /##/
is removed.

But may it work or not -- complain to the creator of your library!

HTH
Heinz


.



Relevant Pages