boost noncopyable实现如下
#ifndef BOOST_NONCOPYABLE_HPP_INCLUDED
#define BOOST_NONCOPYABLE_HPP_INCLUDED
namespace boost {
// Private copy constructor and copy assignment ensure classes derived from
// class noncopyable cannot be copied.
// Contributed by Dave Abrahams
namespace noncopyable_ // protection from unintended ADL
{
class noncopyable
{
protected:
noncopyable() {}
~noncopyable() {}
private: // emphasize the following members are private
noncopyable( const noncopyable& );
const noncopyable& operator=( const noncopyable& );
};
}
typedef noncopyable_::noncopyable noncopyable;
} // namespace boost
#endif // BOOST_NONCOPYABLE_HPP_INCLUDED
比较让我感兴趣的是noncopyable类是定义在noncopyable_命名空间中的,然后在boost命名空间中typedef引出。
为什么这样做呢?注释解释说:protection from unintended ADL,
而这又是什么意思?首先来看看ADL,ADL是”argument dependent lookup”的缩写,ADL依据函数调用中的实参的数据类型查找未限定(unqualified)的函数名(或者函数模板名),具体请查看 依赖于实参的名字查找。
下面是ADL的一个小例子:
namespace N2 { struct DS; };
namespace N1 {
struct S {
void operator+( S) {}
};
template<int X> void f(S) {};
void fDS(N2::DS* v) { };
}
namespace N2 {
struct DS :N1::S {};
template<class T> void f(T t) {};
}
void g() {
N2::DS sv;
fDS(&sv); // sv的类型N2::DS的基类型N1::S所在的命名空间N1的函数N1::fDS
sv+sv; // 调用N1::S::operator+(S)运算符成员函数
}
与上面例子类似,如果我们定义的类继承自noncopyable,同时有一个未限定函数的实参类型是我们定义的类,当调用这个函数时,编译器就会在类类型的命名空间以及其基类所在命名空间查找函数,也就是说,如果没有noncopyable_包裹,编译器便会在boost命名空间里查找这个函数,但这不是我们想要的结果。