之前使用了google的glog来实现obotcha的log存储,但是最近编译的时候发现一个问题,Obotcha的Mutex类和glog的Mutex类冲突了。glog中Mutex定义如下:
#define MUTEX_NAMESPACE glog_internal_namespace_
namespace MUTEX_NAMESPACE {
class Mutex {
public:
// Create a Mutex that is not held by anybody. This constructor is
// typically used for Mutexes allocated on the heap or the stack.
// See below for a recommendation for constructing global Mutex
// objects.
inline Mutex();
// Destructor
inline ~Mutex();
...
};
理论上,我们可以通过namespace来做隔离。但是看代码很奇怪,明明有namespace(MUTEX_NAMESPACE)做了隔离,为什么还会有冲突?结果发现在头文件定义最后,有这么两行:
using namespace MUTEX_NAMESPACE;
#undef MUTEX_NAMESPACE
就是说,只要加载头文件,最后就会自动调用using namespace了,所以实际上namespace的隔离作用根本没有生效。不清楚当时google为什么这么设计,是为了使用者方便?还是处于其他原因?但是,之前我记得看过google的C++编程规范,貌似里面不建议使用using namesapce,最好改用namespace::xxxx这种方式。
关于namespace的用法可以看一下:
https://blog.youkuaiyun.com/wsx199397/article/details/52490300