modern c++ design Int2Type 笔记

[cpp]  view plain copy
  1. #include <cstdlib>  
  2. using namespace std;  
  3.   
  4. class Normal {  
  5.     int _i;  
  6. public:  
  7.     Normal() { _i = 0; }  
  8.     Normal(const Normal& nml) { _i = nml._i; }  
  9.     // other members omitted  
  10. };  
  11.   
  12. class Polym {  
  13.     int *_pi;  
  14.     Polym(const Polym&); // forbids copy-constructor  
  15.     void setp(int* i) { _pi = i; }  
  16. public:  
  17.     Polym() { _pi = 0; }  
  18.     Polym* clone();  
  19.     // other members omitted  
  20. };  
  21. //  
  22. //  part1 codes  
  23. //  
  24. //template<class T, bool isPolymorphic>   
  25. //class NiftyContainer  
  26. //{  
  27. //    // other members omitted  
  28. //public:  
  29. //    void DoSomething()  
  30. //    {  
  31. //        T* pSomeObj =     new T;  
  32. //        if (isPolymorphic)  // branch1   
  33. //        {  
  34. //            T* pNewObj = pSomeObj->clone(); // customized behavior for copying  
  35. //            // some operations on polymorphic object  
  36. //            pNewObj = 0;  
  37. //        }  
  38. //        else  // branch2  
  39. //        {  
  40. //            T* pNewObj = new T(*pSomeObj);  // using copy-constructor  
  41. //            // some operations on non-polymorphic object  
  42. //            delete pNewObj;  
  43. //        }  
  44. //        delete pSomeObj;  
  45. //    }  
  46. //    // other members omitted  
  47. //};  
  48. //  
  49. //int main(int argc, char* argv[])  
  50. //{  
  51. //    // using the container with elements' type Normal  
  52. //    NiftyContainer<Normal, false> nc;  
  53. //    nc.DoSomething(); // error: 类Normal没有成员函数clone   
  54. //      
  55. //    // using the container with elements' type Polym  
  56. //    NiftyContainer<Polym, true> nc2; // error: 类Polym的copy-constructor是private   
  57. //    nc2.DoSomething();  
  58. //      
  59. //    system("PAUSE");  
  60. //    return EXIT_SUCCESS;  
错误解析
// 按照我们的预期,如果元素类型是多态的,那么只执行branch1部分,否则只执行branch2,
// 因为两种类型的元素有不同的拷贝函数。
// 但是编译器可不会理会这些,一律的都要加以编译,因此出错了。
// 
// a 对于模板来说,不同的参数意味着不同的类型。
// b 此外,模板的编译有一个特性,
// 即没有用到的模板函数是不会参与到编译中的,只进行语法正确性的分析。
// 
// c 函数可以根据参数进行重载。 
//  
// 为了做到“根据常数的值(此处就是isPolymorphic)只编译其中某一个分支
// (例如branch1),而不编译其它分支,从而保证最后的代码中只包含需要的代码”, 

// 我们可以利用上述的模板编译特性和函数重载特性。



[cpp]  view plain copy
  1. // part2 codes  
  2. //   
  3. template<int v>  
  4. struct Int2Type { // 创造了这个模板是为了用不同的常数值产生不同的类型   
  5.     enum {value=v};  
  6. };  
  7.   
  8. template<class T, bool isPolymorphic>  
  9. class NiftyContainer  
  10. {  
  11. private:  
  12.     void DoSomething(T* pObj, Int2Type<true>) // function_p   
  13.     {  
  14.         T* pNewObj = pObj->Clone();  
  15.         // some operations on polymorphic object  
  16.         pNewObj = 0;  
  17.     }  
  18.     void DoSomething(T* pObj, Int2Type<false>) // function_n  
  19.     {  
  20.         T* pNewObj = new T(*pObj);  
  21.         // some operations on non-polymorphic object  
  22.     }  
  23. public:  
  24.     void DoSomething(T* pObj)  
  25.     {  
  26.         DoSomething(pObj, Int2Type<isPolymorphic>());  
  27.     }  
  28.     // other members are omitted  
  29. };  
  30.   
  31. int main(int argc, char* argv[])  
  32. {  
  33.     // using the container with elements' type Normal  
  34.     NiftyContainer<Normal, false> nc;// ok  
  35.       
  36.     // using the container with elements' type Polym  
  37.     NiftyContainer<Polym, true> nc2; // ok  
  38.   
  39.       
  40.     system("PAUSE");  
  41.     return EXIT_SUCCESS;  
  42. }  

// 在编译过程中,根据传递的isPolymorphic模板参数值的不同,编译器将选择实现哪个函数。 
// 若为true,则实现function_p,而不会尝试去编译function_n。 
// 这样就成功做到了编译期多态,而不是执行期多态。

// 这个技术的关键点就在于Int2Type模板。 


  1. <pre name="code" class="cpp">#include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4.   
  5. template<class T>  
  6. struct Type2Type  
  7. {  
  8.     typedef T OrgT;   
  9. };  
  10.   
  11. class Widget  
  12. {  
  13.     public:   
  14.         Widget(string str, int n)  
  15.         {         
  16.             cout<<str<<":"<<n<<endl;  
  17.            
  18.         }         
  19.  };  
  20.   
  21.   
  22.  template<class T, class U>  
  23.  T *Create(const U &org, Type2Type<T>)  
  24.  {  
  25.     return new T(org);   
  26.  }  
  27.   
  28. template<class T>  
  29. Widget *Create(const T&org, Type2Type<Widget>)  
  30. {  
  31.     return new Widget(org, -1);   
  32. }  
  33. int main()  
  34. {  
  35.     string *str = Create("hello", Type2Type<string>());  
  36.     cout<<*str<<endl;  
  37.   
  38.     delete str;  
  39.   
  40.     Widget *wid = Create("I am Widget", Type2Type<Widget>());  
  41.   
  42.     delete wid;  
  43. }  
  44. ~                                                                                                                                                       
  45. </pre><br>  
  46. <br>  
  47. <pre></pre>  
  48. <pre name="code" class="cpp"><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; text-align: left; ">一句话总结Type2Type,就是用自定义的空模板,完成了函数重载的工作。</span>  
  49. </pre>  


template <int v>
struct Int2Type
{
    enum { value = v };

};


如下:

template <typename T, bool isPolimorphic>
class NiftyContainer
{
private:
    void DoSomething( T* pObj, Int2Type<true>)
    {
        T* pNewObj = pObj->Clone();
    }

    void DoSomething( T* pObj, Int2Type<false>)
    {
        T * pNewObj = new T(*pObj);
    }

public:
    void DoSomething(T * pObj)
    {
        DoSomething(pObj, Int2Type<isPolimorphic>());
    }

}
;

如果没有这个 Int2Type,是不可能实现两个“重载”函数 DoSomething( .. Int2Type<true>)和 DoSomething(...Int2Type<false>)的,NiftyContainer的参数 isPolimorphic只能取true或者false,实际上编译出来的要么是前一个函数,要么是后一个函数。
如果没有这个Int2Type,最容易想到的就是像下面这样做:

if (isPolimorphic)
{
      T * pNewObj = pSomeObj->Clone();
}
else
{
       T * pNewObj = new T (*pSomeObj);
}

如果多态算法使用 pObj->Clone(),那么对于任何一个未曾定义 Clone函数(即isPolimorphic==false)的类来说,这里的前一个分支是编译失败的。反之,如果 isPolimorphic == true,是不是后面那个分支就保证没问题,一定能通过呢?不是的,如果有一个类设计成为有 Clone函数,但是构造函数是私有的,就不行咯。

所以, Int2Type是跟编译器说:嘿,不要去管那个我不关心的分支。 

妙啊!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值