類型和值之間的映射(Int2Type)

       在C++中,術語“轉化”(conversion)描述的是從另外一個類型的值(value)獲取一個類型(type)的值的過程。可是有時候你會需要一種不同類型的轉化:可能是在你有一個類型時需要獲取一個值,或是其它的類似情形。在C++中做這樣的轉化是不尋常的,因爲類型域和值域之間隔有有一堵很嚴格的界線。可是,在一些特定的場合,你需要跨越這兩個邊界,本欄就是要討論該怎麼做到這個跨越。

映射整數爲類型

一個對許多的generic programming編程風格非常有幫助的暴簡單的模板:

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

對傳遞的每一個不同的常整型值,Int2Type“產生”一個不同的類型。這是因爲不同的模板的實體(instantiation)是不同的類型,所以Int2Type<0>不同於Int2Type<1>等其它的類型的。此外,產生類型的值被“存放”在枚舉(enum)的成員值裏面。

不管在任何時候,只要你需要快速“類型化”(typify)一個整型常數時,你都可以使用Int2Type。

 

問題描述:
當需要根據不同的值選擇不同的行爲時,普通的做法是利用if...else...或者case結構
來實現分支選擇。在一個型別模板化的函數過程中,則可能出現問題。
看下面的第一部分:

 Part 1 
如下的範型容器NiftyContainer的元素型別範型化爲T。在容器中需要實現元素的賦值行爲。非常規的自定義類型,其複製行爲是複雜的。 特別是具有多態特性的類型和普通的類型,其複製過程不一樣。例如,普通類型可以採用逐位複製的簡單方式,而具有多態特性(例如含有指針成員)的類對象則不宜採用 這種方式。此處假定普通類型的複製採用拷貝構造函數方式而多態的類型則採用一個專門的虛函數clone來做拷貝。

  1. #include <cstdlib>
  2. using namespace std;
  3. class Normal {
  4.     int _i;
  5. public:
  6.     Normal() { _i = 0; }
  7.     Normal(const Normal& nml) { _i = nml._i; }
  8.     // other members omitted
  9. };
  10. class Polym {
  11.     int *_pi;
  12.     Polym(const Polym&);                    // forbids copy-constructor
  13.     void setp(int* i) { _pi = i; }
  14. public:
  15.     Polym() { _pi = 0; }
  16.     Polym* clone();
  17.     // other members omitted
  18. };
  19. template<class T, bool isPolymorphic> 
  20. class NiftyContainer
  21. {
  22.    // other members omitted
  23. public:
  24.    void DoSomething()
  25.     {
  26.        T* pSomeObj =new T;
  27.       if (isPolymorphic)                          // branch1 
  28.        {
  29.            T* pNewObj = pSomeObj->clone();   // customized behavior for copying
  30.             // some operations on polymorphic object
  31.             pNewObj = 0;
  32.        }
  33.        else                                     // branch2
  34.         {
  35.            T* pNewObj = new T(*pSomeObj);  // using copy-constructor
  36.            // some operations on non-polymorphic object
  37.           delete pNewObj;
  38.         }
  39.        delete pSomeObj;
  40.     }
  41.    // other members omitted
  42. };
  43. int main(int argc, char* argv[])
  44. {
  45.     // using the container with elements' type Normal
  46.    NiftyContainer<Normal, false> nc;
  47.    nc.DoSomething(); // error: 類Normal沒有成員函數clone 
  48.     
  49.    // using the container with elements' type Polym
  50.     NiftyContainer<Polym, true> nc2; //error: 類Polym的copy-constructor private 
  51.    nc2.DoSomething();
  52.     
  53.    system("PAUSE");
  54.    return EXIT_SUCCESS;
  55. }

錯誤解析
按照我們的預期,如果元素類型是多態的,那麼只執行branch1部分,否則只執行branch2,因爲兩種類型的元素有不同的拷貝函數。但是編譯器可不會理會這些,一律的都要加以編譯,因此出錯了。

 

對於模板來說,不同的參數意味着不同的類型。 此外,模板的編譯有一個特性,即沒有用到的模板函數是不會參與到編譯中的,只進行語法正確性的分析。 函數可以根據參數進行重載。

 

爲了做到“根據常數的值(此處就是isPolymorphic)只編譯其中某一個分支(例如branch1),而不編譯其它分支,從而保證最後的代碼中只包含需要的代碼”, 我們可以利用上述的模板編譯特性和函數重載特性。

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

在編譯過程中,根據傳遞的isPolymorphic模板參數值的不同,編譯器將選擇實現哪個函數。 若爲true,則實現function_p,而不會嘗試去編譯function_n。 這樣就成功做到了編譯期多態,而不是執行期多態。
這個技術的關鍵點就在於Int2Type模板。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章