boost noncopyable实现与ADL
2017-05-06 09:52
399 查看
boost noncopyable实现如下
比较让我感兴趣的是noncopyable类是定义在noncopyable_命名空间中的,然后在boost命名空间中typedef引出。
为什么这样做呢?注释解释说:protection from unintended ADL,
而这又是什么意思?首先来看看ADL,ADL是”argument dependent lookup”的缩写,ADL依据函数调用中的实参的数据类型查找未限定(unqualified)的函数名(或者函数模板名),具体请查看 依赖于实参的名字查找。
下面是ADL的一个小例子:
与上面例子类似,如果我们定义的类继承自noncopyable,同时有一个未限定函数的实参类型是我们定义的类,当调用这个函数时,编译器就会在类类型的命名空间以及其基类所在命名空间查找函数,也就是说,如果没有noncopyable_包裹,编译器便会在boost命名空间里查找这个函数,但这不是我们想要的结果。
#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 { str 4000 uct 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命名空间里查找这个函数,但这不是我们想要的结果。
相关文章推荐
- boost noncopyable实现禁止类拷贝
- boost noncopyable实现禁止类拷贝
- boost noncopyable实现禁止类拷贝
- [转载] boost::noncopyable
- 【Boost】boost::noncopyable介绍
- boost::noncopyable
- boost::noncopyable
- Boost 实用工具:noncopyable、BOOST_BINARY宏、BOOST_CURRENT_FUNCTION宏、bitset类型
- boost 中的noncopyable 【转】
- boost::noncopyable
- 【Boost】boost::noncopyable介绍 .
- c++ boost学习之noncopyable
- boost noncopyable
- boost::noncopyable以及空基类在编程中的应用
- 无法访问 private 成员(在“boost::asio::detail::noncopyable”类中声明)
- boost::noncopyable
- boost noncopyable
- 【Boost】boost::noncopyable介绍
- boost::noncopyable
- boost::noncopyable 禁止拷贝