my textbook encourages us to use using namespace std; after our libriaries. On this forum I'm noticing that some do not. I'm trying to understand what is the reason to use it or not use it, other than having to type std all the time. thanks
my textbook encourages us to use using namespace std; after our libriaries. On this forum I'm noticing that some do not. I'm trying to understand what is the reason to use it or not use it, other than having to type std all the time. thanks
The general rule is that you should not use such a using directive or a using declaration (e.g., using std::cout; ) in a header file except within a restricted scope, or before a header inclusion. Other than that it is a matter of discretion how you want to trade convenience for the risk of a name collision (but there are rare cases where a using declaration is needed, e.g., to unhide names from a base class that may otherwise be hidden). Note that you can still fully qualify names even with a using directive/declaration in effect, and that you can use a namespace alias as an alternative to shorten a long namespace name.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
o.k. thanks!
Only to add a little to what has been said, if you need to "bring in" some names from a namespace for the sake of simplicity or resolution at multiple scopes but for whatever reason can't or do not wish to "bring in" all of the names from a given namespace at the relevant scope you can use a two part process thanks to "using declarations" and "using directives" thus simplifying the process without undue repetition or nasty macros.
Soma
Code:namespace StreamStuff { using stdp::fstream; using stdp::ifstream; using stdp::ofstream; using stdp::iostream; using stdp::istream; using stdp::ofstream; using stdp::stringstream; using stdp::istringstream; using stdp::ostringstream; } void BigFunction1() { using namespace StreamStuff; // a lot of standard stream classes are now in scope from our local port } void BigFunction2() { using namespace StreamStuff; // a lot of standard stream classes are now in scope from our local port } void SomeOtherFunction() { using namespace std; // all standard stuff is now in scope from provider libraries }