Question:
C++ "using namespace std" VS "std::endl, std::string, etc..."?
anonymous
1970-01-01 00:00:00 UTC
C++ "using namespace std" VS "std::endl, std::string, etc..."?
Five answers:
anonymous
2013-03-25 13:17:24 UTC
When you use the std namespace you don't have to use scope "::" to access the functions.



It makes it easier to declare using namespace std than having to type std::cin, std::cout, etc., constantly.
anonymous
2013-03-25 15:49:49 UTC
You should read into why namespaces even exist.



Go on, google "why do namespaces exist".



You should find something about naming collisions. If you write a function called printf, and you include the stdio.h header file...which one gets executed? Well, unless something is wrong, you'll get a bunch of compiler errors about it.



You can quality namespaces so you only use certain classes within that library



using namespace std::cout will make all following cout references refer to what is in the standard library.



My point is that there no right or wrong, but it makes sense that if you are writing code that involves tons and tons of functions with standard names (like sorts) that you would want a way of referring to these functions without naming them something obscure.
anonymous
2013-03-25 13:58:25 UTC
I prefer using the namespace operator. I like to know if I am using the same identifying name as someone else. If compiler gives me an error, I would prefer to investigate the conflicts.



I suppose you could argue that by using the :: scope operator, you are less likely to misspell something and use the wrong function, which could be spelled similarly and has the same return type and takes the same arguments. This messy code environment is probably fubar to begin with.
jake
2013-03-25 13:30:52 UTC
+1 Bigcopee, recommend his best answer.



The only reason your senior programmer likes for you to use it that way, is because later on in advanced topics when you make your own namespaces, you will have to use the namespace accessor "::" , especially if you are dealing with inheritance and multiple libraries. But "using" works just fine 99% of the time. It's more about showing you how it works "underneath the hood of the car".
?
2013-03-25 13:53:31 UTC
There are many names in namespace std. Once you imported the whole thing, you may be caught by surprise when an innocently-named variable "count", "max", or "array" fails to compile due to name collision. And since standard library headers may include each other differently on different platforms, your code may fail to compile for someone else, it stops being portable.



Also, using namespace std makes it harder, for other people, to read your source code. When I see "std::find", I know what this is, when I see "find", I have to check if this is std::find or something you yourself defined.



That said, it's perfectly fine to use "using namespace std;" and even "using namespace boost;" in a small source file. The only rule you should remember is that namespace usings should never appear in a header or before an #include (see C++ Coding Standards by Sutter and Alexandrescu, rule #58)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...