且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么“使用命名空间标准"?被认为是不好的做法?

更新时间:2022-11-16 11:13:56

这与性能完全无关.但是请考虑一下:您正在使用两个名为Foo和Bar的库:

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;

一切正常,您可以从Foo调用Blah(),从Bar调用Quux(),而不会出现问题.但是有一天,您将升级到Foo 2.0的新版本,该版本现在提供了称为Quux()的功能.现在您遇到了冲突:Foo 2.0和Bar都将Quux()导入到全局命名空间中.这将需要花费一些时间来修复,尤其是在函数参数碰巧匹配的情况下.

Everything works fine, and 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.

如果您曾经使用过foo::Blah()bar::Quux(),那么foo::Quux()的引入将是一件不可能的事.

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