views:

319

answers:

4

With using namespace I make the whole contents of that namespace directly visible without using the namespace qualifier. This can cause problems if using namespace occurs in widely used headers - we can unintendedly make two namespaces with identical classes names visible and the compiler will refuse to compile unless the class name is prepended with the namespace qualifier.

Can I undo using namespace so that the compiler forgets that it saw it previously?

+1  A: 

Not to my knowledge... But as a rule I only use "using namespace" in .cpp files.

Martin
+9  A: 

No, but you can tell your coworkers that you should never have a using directive or declaration in a header.

rlbond
+4  A: 

As others said, you can't and the problem shouldn't be there in the first place.
The next-best thing you can do is bring in your needed symbols so that they are preferred by the name look-up:

namespace A { class C {}; }
namespace B { class C {}; }
using namespace A;
using namespace B;

namespace D {
    using A::C; // fixes ambiguity
    C c;
}

In some cases you can also wrap the offending includes with a namespace:

namespace offender {
#  include "offender.h"
}
Georg Fritzsche
That last technique can be a can of worms. If offender.h includes headers that are `#define` protected, now those symbols are stuck in offender. You could try to put its entire interface package comprehensively in a new namespace, but still hope it doesn't include system headers. And if it works once, it might break in the next version.
Potatoswatter
Thus *in some cases*.
Georg Fritzsche
+3  A: 

No, C++ Standard doesn't say anything about "undo". The best you are allowed to do is to limit scope of using:

#include <vector>

namespace Ximpl {

using namespace std;    
vector<int> x;

}

vector<int> z; // error. should be std::vector<int>

But unfortunately using namespace Ximpl will bring all names from std namespace as well.

Kirill V. Lyadvinsky