Why is “using namespace std” considered bad practi

2019-05-21 08:00发布

I've been told by others that writing using namespace std in code is wrong, and that I should use std::cout and std::cin directly instead.

Why is using namespace std considered a bad practice? Is it inefficient or does it risk declaring ambiguous variables (variables that share the same name as a function in std namespace)? Does it impact performance?

30条回答
戒情不戒烟
2楼-- · 2019-05-21 08:27

This is not related to performance at all. But consider this: you are using two libraries called Foo and Bar:

using namespace foo;
using namespace bar;

Everything works fine, you can call Blah() from Foo and Quux() from Bar without problems. But one day you upgrade to a new version of Foo 2.0, which now offers a function called Quux(). Now you've got a conflict: Both Foo 2.0 and Bar import Quux() into your global namespace. This is going to take some effort to fix, especially if the function parameters happen to match.

If you had used foo::Blah() and bar::Quux(), then the introduction of foo::Quux() would have been a non-event.

查看更多
看我几分像从前
3楼-- · 2019-05-21 08:27

It depends on where it is located. If it is a common header, then you are diminishing the value of the namespace by merging it into the global namespace. Keep in mind, this could be a neat way of making module globals.

查看更多
Lonely孤独者°
4楼-- · 2019-05-21 08:28

I recently ran into a complaint about Visual Studio 2010. It turned out that pretty much all the source files had these two lines:

using namespace std;
using namespace boost;

A lot of Boost features are going into the C++0x standard, and Visual Studio 2010 has a lot of C++0x features, so suddenly these programs were not compiling.

Therefore, avoiding using namespace X; is a form of future-proofing, a way of making sure a change to the libraries and/or header files in use is not going to break a program.

查看更多
聊天终结者
5楼-- · 2019-05-21 08:28

Another reason is surprise.

If I see cout << blah, instead of std::cout << blah

I think what is this cout? Is it the normal cout? Is it something special?

查看更多
老娘就宠你
6楼-- · 2019-05-21 08:31

From my experiences, if you have multiple libraries that uses say, cout, but for a different purpose you may use the wrong cout.

For example, if I type in, using namespace std; and using namespace otherlib; and type just cout (which happens to be in both), rather than std::cout (or 'otherlib::cout'), you might use the wrong one, and get errors, it's much more effective and efficient to use std::cout.

查看更多
贼婆χ
7楼-- · 2019-05-21 08:32

One shouldn't use using directive at global scope, especially in headers. However there are situations where it is appropriate even in a header file:

template <typename FloatType> inline
FloatType compute_something(FloatType x)
{
    using namespace std; //no problem since scope is limited
    return exp(x) * (sin(x) - cos(x * 2) + sin(x * 3) - cos(x * 4));
}

This is better than explicit qualification (std::sin, std::cos...) because it is shorter and has the ability to work with user defined floating point types (via Argument Dependent Lookup).

查看更多
登录 后发表回答