Skip to main content
GameDev.net gamedev.net
🔒 Locked

un-using namespace?

Started by aboeing Apr 19, 2004 at 11:01 PM 6 replies 2.1k views
Original Post
aboeing
aboeing
Is there some way to un-use a namespace? So, for example, you have
namespace one {
class vector;
}
namespace two {
class vector;
}

using namespace one;
using namespace two;

vector x; //all confused

The actual using namespace one,two bit isn''t in my code, and it would be a real pain to modify all the code to not have that in it. Are there any other solutions? (something like #undef except for namespaces) (im using vc7.1 if someone wants to give me a compiler-specific solution)
Zipster
Zipster
using obeys scoping rules, so some extra curly braces implicitly define where the using has effect.
antareus
antareus
I don''t think you can, the whole point of namespaces is to prevent those name collisions from occuring. If you can''t then you''re better off explicitly referencing it. It seems like it is asking for a readability issue farther down the line.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Cosmic314
Cosmic314
I don''t think you can "un-use" a namespace. The whole point is to solve the problem you just described. I think you mean to explicitly use:

one::vector x;

But if you wanted the vector in the other namespace you''d do:

two::vector x;
Doc
Doc
I don''t know about "unusing", but "using" a specific class may be helpful. Instead of using namespace you can do "using two::vector;" or "using one::vector" to explicitly tell the compiler which vector you want.
aboeing
aboeing
okay, thanks for the replies guys. I guess i will just try and rewrite the header files.
Zipster: thanks for that tip, hopefully that will cut down some work.
Russell
Russell
You could do stuff like this, and only use using when you are really using the namespace (pun intended).


#include <iostream>

void OutputMyStuff ()
{
using namespace std;
cout << "my stuff" << endl;
}

int main ()
{
OutputMyStuff();
return 0;
}
Teknofreek
Teknofreek
It''s always a good idea to avoid putting "using xyz" in header files. The problem with doing this is that now every file that includes that header automatically inherits that using directive. This can detract from the whole reason to use namespaces in the first place.

-John
John

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.