《C++之那些年踩过的坑(一)》

C++之那些年踩过的(一)

作者:刘俊延(Alinshans)

本系列文章针对我在写C++代码的过程中,尤其是做自己的项目时,踩过的各种坑。以此作为给自己的警惕。


转载请注明原文来自: http://www.cnblogs.com/GodA/p/6501505.html

这一篇就讲点简单的东西,名称空间。

一、谨慎在全局范围使用 using namespace XXX

注意,是在全局范围使用 using namespace XXX,除非写个玩具,否则最好不要这样。之前很喜欢在文件开头就使用 using namespace std,因为可以省很多事。后来我在写 MyTinySTL 这个项目的时候,写测试时为了方便,用了 using namespace std,结果惨不忍睹,大量的名称冲突。当使用 using namepsace xxx 的时候,会把 xxx 这个名称空间里的内容引入到当前空间,如果很不幸,有相同的函数,类等等,就会造成冲突。然而如果碰巧没有冲突,那也只是暂时的,谁知道以后会加入一些什么东西呢?

比如,你写了这样的东西:

#include <iostream>
#include <vector>

namespace your
{

template <typename T>
class vector
{
 public:
  vector() = default;
  ~vector() = default;
 private:
  T* data;
};

}

using namespace std;
using namespace your;

int main()
{
  vector<int> v;
}

很明显,这是编译不过的,同时引入了 std 和 your 两个名称空间,且都有一个 vector 模板类,编译器是不知道要使用哪个的。当然,这个太明显了没有人会这么做。但是,如果代码多了呢?文件多了呢?一不小心在哪个地方用了,也不知道会不会 #include 的时候一层一层的也间接的弄了进来。即使你在某个局部的名称空间xxx内声明了 using namespace std,但是也不能保证哪天谁用你的代码,然后为了方便使用你的东西,using namespace xxx,然后又间接引入了一个 std 导致爆炸。

有很多更好的解决方法。比直接在全局使用 using 声明好一点的是,在局部使用,并且只声明需要的部分。比如有时候我们经常在一个函数/作用域内使用 std::cout, std::endl,那么或许可以这样:

void test()
{
  using std::cout;
  using std::endl;

  cout << "1+1=" << 1 + 1 << endl;
  cout << "1+2=" << 1 + 2 << endl;
  cout << "1+3=" << 1 + 3 << endl;
  // ...
}

上面这样写当然是没问题了啦,不过我现在习惯,并推崇的还是显式声明,一来这样可以更清晰的知道,用的是哪里来的东西,而且几乎放多久都不会错,二来多敲几个键而已,不是什么麻烦事。以上说了很多,都是以 std 为例,对其它的,也应该保持同样的态度,不禁止,但是要谨慎。不过还是养成好习惯比较好,那么就把using namespace std;写成 std::xxx 吧。

放一段C++ Coding Standards 里面的话:

Summary

Namespace usings are for your convenience, not for you to inflict on others: Never write a using declaration or a using directive before an #include directive.

Corollary: In header files, don’t write namespace-level using directives or using declarations; instead, explicitly namespace-qualify all names. (The second rule follows from the first, because headers can never know what other header #includes might appear after them.)

Discussion

In short: You can and should use namespace using declarations and directives liberally in your implementation files after #include directives and feel good about it. Despite repeated assertions to the contrary, namespace using declarations and directives are not evil and they do not defeat the purpose of namespaces. Rather, they are what make namespaces usable.

在放一句 Google C++ Style Guide 里的一条:

You may not use a using-directive to make all names from a namespace available.

二、关于匿名 namespace

关于 unnamed namespace,cppreference 如是说到:

Its members have potential scope from their point of declaration to the end of the translation unit, and have internal linkage.

也就是说,现在,在匿名空间里的成员,具有内部链接。现在来说,匿名空间跟 static 就没区别了。不过依然要注意,在同一层次中,可以有多个匿名空间,不过这些匿名空间会被整合成一个,所以不能像这样写,会报重定义:

#include <iostream>

namespace
{
void foo() { std::cout << "1" << "\n"; }
}

namespace
{
void foo() { std::cout << "2" << "\n"; }
}

int main()
{
  foo();
}

这很容易理解。在不同空间内的匿名函数,就是不同的啦,比如这样就可以通过了:

#include <iostream>

namespace
{
void foo() { std::cout << "1" << "\n"; }
}

namespace n1
{
namespace
{
void foo() { std::cout << "2" << "\n"; }
}
}

int main()
{
  foo();
  n1::foo();
}

Google C++ Style Guide 中,这样讲到匿名namespace:

When definitions in a .cc file do not need to be referenced outside that file, place them in an unnamed namespace or declare them static. Do not use either of these constructs in .h files.

Use of internal linkage in .cc files is encouraged for all code that does not need to be referenced elsewhere. Do not use internal linkage in .h files.

他们鼓励在实现文件中,把那些不需要外部引用的东西放进匿名空间中。陈硕大大在他的 CppPractice 中,第一个提到的就是慎用匿名空间。我觉得,对于他说的不利之处,现在来看,主要还是是第一点。因为匿名namespace里的东西是匿名的,所以万一以后有一天想引用它了,也说不准。其实还是用个具体名称,也不麻烦。对于那些实现细节,或者不希望暴露的,我喜欢扔进一个 namespace details{} 或者什么 namespace impl {} 里。

今天就先谈这么多。总结一下我自己的观点:

  • 使用using namespace xxxxxx::yyy
  • 一般情况下,都使用具名 namespace

原文链接: https://www.cnblogs.com/GodA/p/6501505.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/250228

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午4:20
下一篇 2023年2月14日 上午4:22

相关推荐