Scott Meyes 代码简单剖析完全修正版 2.0
来源:作者: 发布时间:2007-12-05 06:22:25


/*NOTE
请使用g++ 2.95 or higher, Intel C++ 5.0 or higher (6.0 has been released) 编译,其他主流编译器无法正确通过, VC 居然出现 26 处错误 */
#include < iostream > #include < utility > // 含 pair 和 make_pairusing namespace std;
template struct MemFuncTraits { };
//partial specialization 1 template < typename R, typename O > struct MemFuncTraits< R (O::*)() /* 指向成员函数指针 */ > { typedef R ReturnType; typedef O ObjectType; };
//partial specialization 2 template < typename R, typename O > struct MemFuncTraits< R (O::*)() const /* 指向成员函数 const 的指针 */ > { typedef R ReturnType; typedef O ObjectType; };
//partial specialization 3 template < typename R, typename O, typename P1 > struct MemFuncTraits< R (O::*)(P1) /* 指向具备参数类型的成员函数指针 */ > { typedef R ReturnType; typedef O ObjectType; };
//partial specialization 4 template < typename R, typename O, typename P1 > struct MemFuncTraits< R (O::*)(P1) const /* 指向具备参数类型的 const 成员函数指针 */ > { typedef R ReturnType; typedef O ObjectType; };
template < typename MemFuncPtrType > class PMFC { public: typedef typename MemFuncTraits::ObjectType ObjectType; typedef typename MemFuncTraits::ReturnType ReturnType; typedef std::pair CallInfo; PMFC(const CallInfo& info) : _callinfo(info) { } //init
// 支持无参数类型,ReturnType = MemFuncPreType ReturnType operator()() const { return (_callinfo.first->*_callinfo.second)(); }
// 支持具备参数类型 template ReturnType operator()(Param1Type p1) const { return (_callinfo.first->*_callinfo.second)(p1); } private: CallInfo _callinfo; };
template class SmartPtrBase { public: SmartPtrBase(T *p) : ptr(p) { } //init
//partial specialization PMFC 并 重载 operator->*. template const PMFC operator->*(MemFuncPtrType pmf) const { return std::make_pair(ptr, pmf); } private: T* ptr; };
template class SP : private SmartPtrBase { // g++ 3.04 都有错误,改为 public,估计是个 bug。Intel C++没有错误。public: SP(T *p) : SmartPtrBase(p) { } using SmartPtrBase::operator->*; };
class Wombat { public: int dig() { cout << "Digging..." << endl; return 1; } int sleep() { cout << "Sleeping..." << endl; return 5; } int eat() const { cout << "Eatting..." << endl; return 7; } int move(int op) { cout << "Moving..." << endl; return 9; } };
typedef int (Wombat::*PWMF)(); typedef int (Wombat::*PWMFC)() const; typedef int (Wombat::*PWMF1)(int);
main() { SP pw = new Wombat; PWMF pmf = &Wombat::dig; (pw->*pmf)(); // Digging... pmf = &Wombat::sleep; (pw->*pmf)(); // Sleeping... PWMFC pmfc = &Wombat::eat; (pw->*pmfc)(); // Eatting... PWMF1 pmf1 = &Wombat::move; (pw->*pmf1)(2); // Eatting... return 0; }
|
还没有关于此文章的相关评论!