Sign in to follow this  
rippingcorpse

using namespace std is frowned upon?

Recommended Posts

Why is "using namespace std" generally considered a newbie way of programming? I read this online and I've been wondering. I hope I'm not too much of an annoyance with all these questions, you've all been a real help!

Share this post


Link to post
Share on other sites
Well first of all, you are asking this question in the beginners forum, and no question is to annoying (unless you can find the answer by searching Google).

To answer your question, the "using namespace std;" is not a newbie way of programming. If you are just starting to learn C++, then it is a great way to start. Of course, when you get into more advanced programs, you will need to learn to program without it, but it is a great place to start.

Share this post


Link to post
Share on other sites
Quote:
Original post by rippingcorpse
Why is "using namespace std" generally considered a newbie way of programming? I read this online and I've been wondering. I hope I'm not too much of an annoyance with all these questions, you've all been a real help!


using namespace std; includes all of the declarations and definitions within the std namespace (Which is large) -- do to the use of the using keyword -- into the current scope (in your case, it would be the global namespace). This has alot of problems in that it increases compile times, clutters up the global namespace (Making it harder to avoid name collisions).

Namespaces help us avoid cluttering up the global namespace, however this becomes meaningless if you introduce them into the global namespace.

---

As themagicalrock pointed out, it is not coinsidered the "newbie" way. I personally regard it as a poor construct, however, to use on the std namespace for the reasons described above. This is my personal preference, though.

Share this post


Link to post
Share on other sites
Using namespace directives are fine, in their place. Their place isn't header files. The reason is simple, every file which now includes that file will also be using any namespaces "imported" by such a directive. This effectively undermines the very reason for having namespaces.

In a source file, you may use such a directive all you want. After all, you never #include a source file.

It is mainly a stylistic issue when you are talking about a one-person project. However, if you ever intend to release your code to others (for example, you are making a library of some kind), then it becomes quite important. We mainly mention it here to get new C++ programmers used to the concept.

I used to place "using" directives in every file, now I never do. Believe me, you get used to typing "std::" or "boost::" quickly from the header files. And when you start implementing said functions in the source files, you are likely to copy/paste the function signatures from the header anyway. If you are smart, you will probably have typedef'd some of the longer names in the class too. Before you know it, you will be automatically filling out the namespace for every type, or be using a short typedef name. I use it even when typing up replies here on gamedev, even if I am modifying someone's quoted source who has a using directive.

Share this post


Link to post
Share on other sites
The reason that it's generally not used is because the std namespace is large. One normally only needs a few things out of the namespace, but including the entire namespace only to use those few things is a waste of resources; you don't need to include the whole thing. Therefore, rather than including the entire namespace, it's better to include the individual pieces that you want, i.e.

using std::string;
using std::cout;
using std::cin;

instead of

using namespace std;

Share this post


Link to post
Share on other sites
To be honest I'm not entirely convinced that every member on gamedev.net that says that using stl is bad actually knows why. I've seen programmers say it to beginners before without giving appropriate justification.

On a literal basis, 'using namespace std;' is bad in header files because whenever that header is included in your program the std namespace gets imported which essentially pollutes the global namespace, which defeats the point of having namespaces in the first place. Much better to keep this code to your implementation files, which is probably going to need it to save many finger aches.

On a more theoretical basis using stl is a bad thing for seasoned professionals for quite a few reasons. It may be a while until you've learned enough to fully appreciate why, but in case you're interested the best reference I know on it is here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html. Though I agree with themagicalrock, for beginners it's essential that you learn how to use stl, as it is the key to unlocking a lot of the power of C++. Stl is also very well suited to application development, and either way, you'll probably end up doing a fair bit of that sooner or later.

Hope this helps! (And it looks like everyone else beat me to it ;-))

Share this post


Link to post
Share on other sites
Quote:
Original post by Crypter
using namespace std; includes all of the declarations and definitions within the std namespace (Which is large) into the current scope...

Not exactly.

The using directive adds a namespace or symbol to the default search space, but the actual search (and traversal of available symbols and namespaces) occurs at the point of symbol resolution, not due to the directive. What this means is that adding a using namespace std; to a file in which none of its symbols are used and there are no collisions incurs a small, and potentially zero, penalty.

Nevertheless, it is good practice to only include those symbols/namespaces that are truly needed. Further, it is good practice to only use namespaces in implementation (.cpp) files for reasons others have covered.

Share this post


Link to post
Share on other sites
Quote:
Original post by TheGilb
To be honest I'm not entirely convinced that every member on gamedev.net that says that using stl is bad actually knows why.

Obviously, because using the STL (or, more correctly, Standard C++ Library or SC++L) isn't bad. Unless by "using stl" you actually meant "using namespace std" or something crazy like that.

Share this post


Link to post
Share on other sites
Quote:
Original post by TheGilb
To be honest I'm not entirely convinced that every member on gamedev.net that says that using stl is bad actually knows why. I've seen programmers say it to beginners before without giving appropriate justification.


This wasn't mentioned anywhere in this thread. It was about "using namespace std;" and the resulting namespace polution.

Nothing involving standard library. I believe the general consensus these days is that standard library came a long way in terms of portability and performance, and that it should be primary choice.

But I really can't recall anyone saying that using STL is bad, unless this
Quote:
On a more theoretical basis using stl is a bad thing for seasoned professionals for quite a few reasons
should be interpreted as such, but that shall remain something we don't necessarily agree upon.

Share this post


Link to post
Share on other sites
Actually, clutter isn't the primary reason for avoiding using directives or declarations in headers. The primary reason is that put a using directive or declaration in a header can change the meaning of subsequent code.

For example, let's consider this sample code:

#include <iostream>
#include <d3d9.h>
#include <d3dx9.h>

void rotate(D3DMATRIX * first, D3DMATRIX * second, D3DMATRIX * third) {
std::cout << "my matrix rotate" << std::endl;
// do stuff
}

int main(int, char **) {
D3DXMATRIX a, b, c;
rotate(&a, &b, &c);
}

When run this program should output "my matrix rotate". Now let's create a header that's just:

#include <algorithm>
using namespace std;

Add an include for this file and rerun the program. Now there isn't any input. This is because now the std::rotate() function is being called instead of the program's ::rotate() function. Since D3DXMATRIX derives from D3DMATRIX, the overload resolution for the ::rotate() function isn't an exact match. However, the template std::rotate<D3DXMATRIX *> is an exact match, so it gets called in preference to ::rotate().

Using declarations are bad in headers for a different reason: using declarations are subject to order dependence issues. Using declarations like using std::swap; pull names into the current scope based only on what names are visible at the point of the using declaration. Additional declarations after the using declaration will be ignored for the purpose of the using declaration. For example:

// header 1
namespace A {
void foo(short) { std::cout << "foo(short)" << std::endl; }
}

using A::foo;

// header 2
namespace A {
void foo(int) { std::cout << "foo(int)" << std::endl; }
}

// source file
int main(int, char **) {
foo(1);
A::foo(1);
}

In this program, the two calls to foo() and A::foo() will print different message if header 1 is included before header 2. However, if header 2 is included before header 1, then both calls to foo() will result in the same output. Code that changes meaning based on what order you include headers is a bad thing.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this