'Should I use <code>using namespace std;</code> in my code?'
-
I've put the topic in quotes, because I'm not asking for me. Instead I am referring to the various occasions I've seen where people advise inexperienced programmers to add
using namespace std;
to their program. Personally I feel that this is not generally a good advice, for all of the reasons pointed out at http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5[^]. But maybe the people offering this advice do have their reasons and take a different view? I'd like to know. What is your stance? Why do you think it is good to add a
using namespace
statement, and when? Or do you agree with me and generally advise against it? If so, can you offer any reasons not already offered at the site I linked above? P.S.: After reading the responses I've found there are indeed (IMHO) few strong arguments in favor of using
using namespace std;
. The two main arguments were: 1. Typing effort 2. Lack of readability, most notably (but not only) with the use of formatting in stream classes Personally I don't think the typing effort should be considered a valid reason, unless there are absolutely no drawbacks to consider. Apart from that, both cases can be countered by various methods: 1. Use intelligent editors to minimize the typing effort 2. Usetypedef
to simplify complex (or just hard to type) expressions 3. Use a using declaration (e. g.using std::cout;
) rather than a using directive In those rare cases where, locally, symbols from namespace std are being heavily used (e. g. an output function using lots of formatting), adding a using directive locally (i. e. within the scope of the function only) could be a reasonable compromise. Thanks to Stephen Hewitt for pointing that out. -
I've put the topic in quotes, because I'm not asking for me. Instead I am referring to the various occasions I've seen where people advise inexperienced programmers to add
using namespace std;
to their program. Personally I feel that this is not generally a good advice, for all of the reasons pointed out at http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5[^]. But maybe the people offering this advice do have their reasons and take a different view? I'd like to know. What is your stance? Why do you think it is good to add a
using namespace
statement, and when? Or do you agree with me and generally advise against it? If so, can you offer any reasons not already offered at the site I linked above? P.S.: After reading the responses I've found there are indeed (IMHO) few strong arguments in favor of using
using namespace std;
. The two main arguments were: 1. Typing effort 2. Lack of readability, most notably (but not only) with the use of formatting in stream classes Personally I don't think the typing effort should be considered a valid reason, unless there are absolutely no drawbacks to consider. Apart from that, both cases can be countered by various methods: 1. Use intelligent editors to minimize the typing effort 2. Usetypedef
to simplify complex (or just hard to type) expressions 3. Use a using declaration (e. g.using std::cout;
) rather than a using directive In those rare cases where, locally, symbols from namespace std are being heavily used (e. g. an output function using lots of formatting), adding a using directive locally (i. e. within the scope of the function only) could be a reasonable compromise. Thanks to Stephen Hewitt for pointing that out.Well, if you have lots of code using funcs only form one namespace in a file, then you can reduce the amount of typing you have to do by stating 'using namespace' at the top of the file. If you are not, and you are mixing lots of code from different namespaces, then you shouldnt, because you might have a clash somewhere, and either compiler errors or runtime errors.
============================== Nothing to say.
-
I've put the topic in quotes, because I'm not asking for me. Instead I am referring to the various occasions I've seen where people advise inexperienced programmers to add
using namespace std;
to their program. Personally I feel that this is not generally a good advice, for all of the reasons pointed out at http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5[^]. But maybe the people offering this advice do have their reasons and take a different view? I'd like to know. What is your stance? Why do you think it is good to add a
using namespace
statement, and when? Or do you agree with me and generally advise against it? If so, can you offer any reasons not already offered at the site I linked above? P.S.: After reading the responses I've found there are indeed (IMHO) few strong arguments in favor of using
using namespace std;
. The two main arguments were: 1. Typing effort 2. Lack of readability, most notably (but not only) with the use of formatting in stream classes Personally I don't think the typing effort should be considered a valid reason, unless there are absolutely no drawbacks to consider. Apart from that, both cases can be countered by various methods: 1. Use intelligent editors to minimize the typing effort 2. Usetypedef
to simplify complex (or just hard to type) expressions 3. Use a using declaration (e. g.using std::cout;
) rather than a using directive In those rare cases where, locally, symbols from namespace std are being heavily used (e. g. an output function using lots of formatting), adding a using directive locally (i. e. within the scope of the function only) could be a reasonable compromise. Thanks to Stephen Hewitt for pointing that out.I personally almost never use it, simply because it clutters the global namespace. The worst abuse is to put it in a header file, and thus, polluting the global namespace in all files including that header. I imagine though it can be useful in some situations, like when dealing with streams and manipulators. The code tends to get quite verbose if it's fully qualified. A local
using namespace
could be acceptable in my oppinion. Good topic by the way. -
I personally almost never use it, simply because it clutters the global namespace. The worst abuse is to put it in a header file, and thus, polluting the global namespace in all files including that header. I imagine though it can be useful in some situations, like when dealing with streams and manipulators. The code tends to get quite verbose if it's fully qualified. A local
using namespace
could be acceptable in my oppinion. Good topic by the way.Niklas Lindquist wrote:
when dealing with streams and manipulators
Yes, I do find myself writing
std::cout
andstd::endl
quite a lot, but so far I've resisted the urge to add ausing
statement. However, you could insert a using declaration instead of a using directive for the most commonly used symbols, as pointed out at C++ FAQ.Niklas Lindquist wrote:
The code tends to get quite verbose
Are you implying this is a bad thing? I actually find that helpful. Besides, the extra typing is not actually an issue, thanks to intelligent editor tools.
-
Niklas Lindquist wrote:
when dealing with streams and manipulators
Yes, I do find myself writing
std::cout
andstd::endl
quite a lot, but so far I've resisted the urge to add ausing
statement. However, you could insert a using declaration instead of a using directive for the most commonly used symbols, as pointed out at C++ FAQ.Niklas Lindquist wrote:
The code tends to get quite verbose
Are you implying this is a bad thing? I actually find that helpful. Besides, the extra typing is not actually an issue, thanks to intelligent editor tools.
Stefan_Lang wrote:
The code tends to get quite verbose
Are you implying this is a bad thing?
In most cases, no. In some cases, yes. It does affect readability if it's too frequent. However, I have not experienced this in any other situation than when working with std streams.
-
I've put the topic in quotes, because I'm not asking for me. Instead I am referring to the various occasions I've seen where people advise inexperienced programmers to add
using namespace std;
to their program. Personally I feel that this is not generally a good advice, for all of the reasons pointed out at http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5[^]. But maybe the people offering this advice do have their reasons and take a different view? I'd like to know. What is your stance? Why do you think it is good to add a
using namespace
statement, and when? Or do you agree with me and generally advise against it? If so, can you offer any reasons not already offered at the site I linked above? P.S.: After reading the responses I've found there are indeed (IMHO) few strong arguments in favor of using
using namespace std;
. The two main arguments were: 1. Typing effort 2. Lack of readability, most notably (but not only) with the use of formatting in stream classes Personally I don't think the typing effort should be considered a valid reason, unless there are absolutely no drawbacks to consider. Apart from that, both cases can be countered by various methods: 1. Use intelligent editors to minimize the typing effort 2. Usetypedef
to simplify complex (or just hard to type) expressions 3. Use a using declaration (e. g.using std::cout;
) rather than a using directive In those rare cases where, locally, symbols from namespace std are being heavily used (e. g. an output function using lots of formatting), adding a using directive locally (i. e. within the scope of the function only) could be a reasonable compromise. Thanks to Stephen Hewitt for pointing that out.i'll use it in a .cpp unless i'm feeling particularly masochistic that day and want to type "std::" a hundred times.
-
Stefan_Lang wrote:
The code tends to get quite verbose
Are you implying this is a bad thing?
In most cases, no. In some cases, yes. It does affect readability if it's too frequent. However, I have not experienced this in any other situation than when working with std streams.
I agree... it can affect readability...
-
I've put the topic in quotes, because I'm not asking for me. Instead I am referring to the various occasions I've seen where people advise inexperienced programmers to add
using namespace std;
to their program. Personally I feel that this is not generally a good advice, for all of the reasons pointed out at http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5[^]. But maybe the people offering this advice do have their reasons and take a different view? I'd like to know. What is your stance? Why do you think it is good to add a
using namespace
statement, and when? Or do you agree with me and generally advise against it? If so, can you offer any reasons not already offered at the site I linked above? P.S.: After reading the responses I've found there are indeed (IMHO) few strong arguments in favor of using
using namespace std;
. The two main arguments were: 1. Typing effort 2. Lack of readability, most notably (but not only) with the use of formatting in stream classes Personally I don't think the typing effort should be considered a valid reason, unless there are absolutely no drawbacks to consider. Apart from that, both cases can be countered by various methods: 1. Use intelligent editors to minimize the typing effort 2. Usetypedef
to simplify complex (or just hard to type) expressions 3. Use a using declaration (e. g.using std::cout;
) rather than a using directive In those rare cases where, locally, symbols from namespace std are being heavily used (e. g. an output function using lots of formatting), adding a using directive locally (i. e. within the scope of the function only) could be a reasonable compromise. Thanks to Stephen Hewitt for pointing that out.It's worth noting that it can be used in any scope, not just globally. For example, you could use it in a function like this:
void DoStuff()
{
using namespace std;
cout << "Yeah!" << endl;
}You can also selectively "lift" something into a namespace with a using declaration:
void DoStufff()
{
using std::cout;
using std::endl;
cout << "Yeah!" << endl;
}Steve
-
Niklas Lindquist wrote:
when dealing with streams and manipulators
Yes, I do find myself writing
std::cout
andstd::endl
quite a lot, but so far I've resisted the urge to add ausing
statement. However, you could insert a using declaration instead of a using directive for the most commonly used symbols, as pointed out at C++ FAQ.Niklas Lindquist wrote:
The code tends to get quite verbose
Are you implying this is a bad thing? I actually find that helpful. Besides, the extra typing is not actually an issue, thanks to intelligent editor tools.
Stefan_Lang wrote:
Are you implying this is a bad thing?
I think it clutters the code. Anyone puzzled by (for instance) STL error messages could share my opinion.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Well, if you have lots of code using funcs only form one namespace in a file, then you can reduce the amount of typing you have to do by stating 'using namespace' at the top of the file. If you are not, and you are mixing lots of code from different namespaces, then you shouldnt, because you might have a clash somewhere, and either compiler errors or runtime errors.
============================== Nothing to say.
I don't consider the typing to be a problem - good editors will reduce the extra typing to a minimum, and even in Notepad there's nothing preventing you from copy-pasting the extra
std::
repeatedly. I do it all the time. (the copy-pasting, not using Notepad ;) ) P.S.: You could also usetypedef
s to improve readability and reduce typing effort. This can be especially helpful for containers and iterators. -
Stefan_Lang wrote:
Are you implying this is a bad thing?
I think it clutters the code. Anyone puzzled by (for instance) STL error messages could share my opinion.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Ummm, I've never checked, but do error messages look different depending on whether or not you have a
using
directive? :confused: -
It's worth noting that it can be used in any scope, not just globally. For example, you could use it in a function like this:
void DoStuff()
{
using namespace std;
cout << "Yeah!" << endl;
}You can also selectively "lift" something into a namespace with a using declaration:
void DoStufff()
{
using std::cout;
using std::endl;
cout << "Yeah!" << endl;
}Steve
Hey, thanks, I actually never thought of that! Yes, I suppose if you have some dedicated IO functions it would be quite reasonable to add a using
directive
in there. Good idea! :) -
Ummm, I've never checked, but do error messages look different depending on whether or not you have a
using
directive? :confused:There's no relation, of course. My point of view is: using
std::
everywhere simply makes code look more cluttered and 'cluttered-looking' code is bad (as you may see in the 'really messy'STL
error messages).If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
There's no relation, of course. My point of view is: using
std::
everywhere simply makes code look more cluttered and 'cluttered-looking' code is bad (as you may see in the 'really messy'STL
error messages).If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]I've always thought that it's mostly the stuff that isn't also visible in the code, that makes compiler errors so hard to read, whereas the namespace symbols help separate the mangled symbols into managable bits that can be easier to understand. Personally, when I look at unfamiliar code and try to locate a problem, seeing
std::
or the like helps me exclude parts that I know I don't need to bother about. Similarly, when I try to understand what a particular piece of code does, I often start with the bits that call out 'STL' to me, because that is what I have the least trouble to grasp. In short, to me the namespace qualifiers within the code are more helpful than obstructive. Of course, it might depend on the code you're looking at. If you're faced with types likestd::list<std::pair<MyClassA, MyClassB>, MyAllocator<std::pair<MyClassA, MyClassB> >::const_iterator
then you're in need of a typedef or two ;)
-
I don't consider the typing to be a problem - good editors will reduce the extra typing to a minimum, and even in Notepad there's nothing preventing you from copy-pasting the extra
std::
repeatedly. I do it all the time. (the copy-pasting, not using Notepad ;) ) P.S.: You could also usetypedef
s to improve readability and reduce typing effort. This can be especially helpful for containers and iterators.