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?
7 Answers 7
No, but you can tell your coworkers that you should never have a using
directive or declaration in a header.
3 Comments
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.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"
}
1 Comment
#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.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.
Comments
Not to my knowledge... But as a rule I only use "using namespace" in .cpp files.
Comments
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
Comments
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.
}
Comments
- The thread is aged, though @rlbond might have separated "namespace declaration" from "using directive", so obviously I had to ponder here.
- @Georg Fritzsche encourages toxic environ with naming his "offender". In the least, you found any work product something business-disagreeable.
- 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.
1 Comment
using namespace std
just be selective: using std::vector; using std::string;
and you'll be swatting many fewer mosquitoes.
#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 useN
anywhere in the file you don't wantnamespace::
to be. Atypedef
could potentially be useful as well.