I was just wondering isA good idea or just program without it?Code:using namespace std;
I was just wondering isA good idea or just program without it?Code:using namespace std;
(Dante)"Blood you have thirsted for -- now, drink your own!"
That's a question for debate, really. There is no right or wrong answer, but rather it is a "it depends".
My personal view is that it is fine to use "using namespace std" at the beginning of a C file. Or "using namespace X" for that matter.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
if you know there aren't and won't be any name conflicts, go for it. if you aren't sure, don't take the risk. simple.
My opinion is to use it if you have to, otherwise save yourself the keystrokes.
It is generally bad form to put the std using directive in header files or above other header #includes. So except for simple programs I'd stay away from doing that.
As for using it in source files, that is up to you. I prefer not to do so but there are valid arguments on both sides.
Indeed & completely agreed - using directives (std or otherwise) should never[1] be found in header-files, because it "messes" with what happens in code that includes that header file beyond what you can expect.
[1] I would consider exceptions to this that you may for example implement some sort of replacement functions for some standard functions, and thus do something like:
So if you include "mydebug.h" you get some debugging version of the standard function, otherwise you get the normal version. But that is a very special case.Code:// mydebug.h using MyDebug::somefunc; ...
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I would encourage you to use 'using' directives.
I would even encourage you to use 'using namespace' directives.
I'll even tell you that you will eventually have to use a 'using' directive.
The problems come from putting such a directive in a header or at the top of source files from global scope. It is evil. If you have ambiguities, collisions, you must fully qualify the name. Any 'using' directive can cause these ambiguities. If you must fully qualify the name there is no point to namespaces. If it comes to that, you may as well just be using the C style 'long_name_prefix'.
I've always used the 'local' or '<anonymous>::local' "trick" to get just what I need to implement the source for a particular translation unit.
[Edit]
This is silly. Or maybe you just don't mean what you said. If the directive is enclosed in some private scope it shouldn't cause any problems.using directives (std or otherwise) should never[1] be found in header-files
If you are dealing with templates and you need a function symbol to resolve to non-standard overloads you have no choice.
[/Edit]
Soma
Code:namespace { namespace local { using foo1::bar1; using foo1::bar2; using foo2::bar3; using foo2::bar4; using namespace foox::barx; } } fully::qualified::type some::other::name() { using namespace local; // ... bar1 b1; bar2 b2; bar3 b3; bar4 b4; // ... barx1 bx1; // ... }
Last edited by phantomotap; 02-09-2009 at 03:36 PM.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
Would you care to elaborate on that? I've never seen any situation where you couldn't just fully qualify the name to get it working.Of course, that doesn't justify putting them in the global namespace, but phantomatap didn't say that.Code:namespace foo { class MyComplexObject { ... }; inline void swap(MyComplexObject &l, MyComplexObject &r) { /* fast swap */ } } namespace bar { template <typename T> void MyTContainer<T>::swap_with_back(T& t) { std::swap(t, this->back()); // Oops, doesn't use the fast swap for MyComplexObject. } template <typename T> void MyTContainer<T>::swap_with_back(T& t) { using std::swap; swap(t, this->back()); // Ah, ADL finds and uses the fast swap. } }
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
I personally prefer to only "using" the things I need.
Less pollution, and no possibility of someone introducing something into the std namespace in a later standard and break my code.Code:using std::cin; using std::cout; using std::vector; ... etc
You can just call the former using declarations and the latter using directives.Originally Posted by phantomotap
My preferred example of when a using declaration would be appropriate in a restricted scope in a header file would be to bring in names from a base class hidden by overloads in a derived class.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I am typically against using directives, since I like to use them as categories. If I put functions inside a namespace String, I won't have to prefix those functions "Str" or such.
So I have this:
And then do...Code:namespace String { std::string Cut(std::string&, std::string&); }
...instead of...Code:using namespace Strings; Cut(...);
Which is more readable?Code:String::Cut(...);
It can get worse with other functions such as if you have functions named "Load" or similar.
But using isn't evil. Oh no. There is a time where it can be applied that gives more readability. I'm just saying one should be careful in doing so, and that fully expressing them isn't so bad as it sounds, and we're not back in the C era.
The thing with using namespaces as categories is that it wouldn't really avoid conflicts, would it? If your library puts math-related functions into a namespace called Math and my library does the same since that's the most logical thing to do in order to categorize things, there would still be naming conflicts?
On the other hand (top-level) namespaces like std or boost don't categorize things and just try to be library-specific...
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Well, yes.
But since I don't really care that it is YourLibrary that I'm using (because the only math library I am using is yours, whereas I might care more that it is the math module of YourLibrary) why shouldn't I do this in source files?
Code:namespace my_library { namespace math { void sqrt() {} } } using namespace my_library; int main() { math::sqrt(); }
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.