35

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?

asked Jan 28, 2010 at 7:23
3
  • I bet there's a really ugly hack using the pre-processor for this. But I guess you don't want that Commented Jan 28, 2010 at 7:28
  • 6
    @Eli: There isn't in Boost, which probably means that there isn't one. Commented Jan 28, 2010 at 7:34
  • A possible solution to at least shorten what you have to type would be to #define N namespace:: at the top of a file and #undef N at the bottom. Of course this then means you have to be careful to never use N anywhere in the file you don't want namespace:: to be. A typedef could potentially be useful as well. Commented Oct 13, 2015 at 18:56

7 Answers 7

44

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

answered Jan 28, 2010 at 7:28

3 Comments

Unfortunately, a third party library (opencv.org/platforms/cuda.html) requires this and we cannot rewrite it. This does not look like an answer, many things should be avoided in the life but are inevitable.
@AudriusMeškauskas being impractical doesn't make it incorrect.
I see nothing wrong with a using declaration in a header, for example when used in a similar way to a typedef, or for a class to import some base class functions within a class definition (e.g., using Base::Base;). It's using namespace that is ill-advised in headers.
18

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"
}
answered Jan 28, 2010 at 7:33

1 Comment

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.
8

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.

answered Jan 28, 2010 at 7:46

Comments

3

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

answered Jan 28, 2010 at 7:27

Comments

1

The closest, that I'll try to use in header files is following:

//example.h
#ifndef EXAMPLE_H_
#define EXAMPLE_H_
/**
 * hating c++ for not having "undo" of using namespace xx
 */
#define string std::string
#define map std::map
class Example {
public:
 Example (const char *filename);
 Example (string filename);
 ~Example ();
private:
 map<string,complicated_stuff*> my_complicated_map;
};
#undef string
#undef map
#endif //EXAMPLE_H_

after all, defines are #undef -able. There are 2 problems: 1. it is ugly 2. separate #define and #undef for each name from the corresponding namespace are used

answered Dec 13, 2013 at 8:35

Comments

0

As stated you should not use using namespace sth in header files. When you need functionality from a namespace in your implementation you can leverage scopes like this:

void func() {
 // some code agnostic to your namespace.
 {
 using namespace sth;
 // some code aware of sth.
 }
 // some other code agnostic to your namespace.
}
answered Mar 6, 2022 at 4:41

Comments

0
  1. The thread is aged, though @rlbond might have separated "namespace declaration" from "using directive", so obviously I had to ponder here.
  2. @Georg Fritzsche encourages toxic environ with naming his "offender". In the least, you found any work product something business-disagreeable.
  3. Personally, I do not like sprinkling "std::" everywhere (it becomes a swarm of mosquitoes), though I agree with @rlbond with avoiding any using namespace clause within any header. Separating execution code away from header files (as: establishing a link library) is always good programming practice.
answered Apr 10, 2023 at 23:54

1 Comment

Rather than using namespace std just be selective: using std::vector; using std::string; and you'll be swatting many fewer mosquitoes.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.