We import namespace into scope by "using" keyword. Is it possible to kick it out of scope?
We import namespace into scope by "using" keyword. Is it possible to kick it out of scope?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Im not sure, but try looking at the header library files with your compiler. If you can understand what they mean, try impementing a function that might do the opposite. Of course, a compiler has loads of header files with it, but try namespace.h or even winproc.h.
Something like that?
Actually just typing std:: (or any other namespace identifier) each time where needed quickly becomes a second nature. And it makes code clearer.Code:#include <iostream> namespace John { void Message() { std::cout << "John's message!" << std::endl; } } namespace Mary { void Message() { std::cout << "Mary's message!" << std::endl; } } using John::Message; //global namespace int main() { Message(); //John:: { using Mary::Message; //in this scope Message(); //Mary:: } Message(); //John:: std::cin.get(); }
Something like thisCode:using namespace std; //code unuse namespace std;
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
I don't think it is possible. A namespace is essentially the introduction of a scope. The using directive (using namespace foo) brings the whole of that scope into view. Only by leaving the scope at which the using directive was used will you essentially remove access to it.
The same happens to the using declaration (using foo:bar). It brings a name into view at the current scope. You will need to leave that scope to remove that name from view.
Creating nested scopes will not help because, as you probably already know, scopes carry on downwards.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
You should just start typing std::cout, std::cin, std::string etc. If you are writing your own header files, you are strongly discouraged to use global using after all. And it's not as inconvenient as it may seem.
Then you won't have problems that you might have.
It is exactly what I want to escape from and reason of this thread:-).You should just start typing std::cout, std::cin, std::string etc. If you are writing your own header files, you are strongly discouraged to use global using after all.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Escaping good practices tends to lead to problems
If you have some identifiers that are conflicting with things in std namespace, you could put them in a separate namespace and use them with the namespace identifier (e.g my::cout) when they are needed.
Or just avoid conflicting names?Code:#include <iostream> using namespace std; namespace my { int cout; } int main() { my::cout = 5; cout << my::cout << endl; cin.get(); }
It is just a technical question. I don't have a real world problem.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
I think he wants it so that he can use a namespace in one place (like a header), then unuse it so it doesn't affect whatever program he's writing; where he may be using another namespace.
I've no idea I'm afraid.
I need a new keyword: "dontuse"
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Other than introducing scopes into the code there is no other way.
Code:int main() { int a = 12; std::cout << a << '\n'; // Ok { using namespace std; cout << a << '\n'; // Ok } cout << a << '\n'; \\ Error! }
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
I reckon the only way to unuse is to not use...
>It is just a technical question. I don't have a real world problem.
Namespaces aren't like macros. If you use them correctly, the hypothetical problem you're talking about simply doesn't exist.
My best code is written with the delete key.
Explicit scoping, just do it.
Lame.Code:#include <iostream> namespace Mary { void message() { std::cout << "Mary says hello.\n"; } } namespace John { void message() { std::cout << "John says hello.\n"; } } namespace Walter { void message() { std::cout << "Walter says hello.\n"; } } int main() { Walter::message(); John::message(); Mary::message(); }