利用C++模板靜態多態性

原文鏈接:http://blog.csdn.net/gong_xucheng/article/details/5790634

轉自http://blog.csdn.net/gong_xucheng/article/details/5790634

  熟悉模板編程的朋友或許聽到過這個技巧或者模式:Barton-Nackmann 技巧或者稱 奇異循環模板模式(Curiously Recurring Template Prattern)。
     其實在 《c++ 編程語言》這本bible 書裏,在模板那章提到過一個很奇妙的類的實現,用的就是這個技術。當時,我就被C++模板技術歎爲觀止。近期在學boost庫時偶然碰到了這個技巧,同時在寫一個類時引發了我的思考,這裏就利用這個技巧來實現,靜態多態函數(我自己發明的叫法,呵呵)。
 我們知道C++的多態函數會帶來很多靈活性,但是不可避免的它是有運行時的性能損失的。而c++的另一個強大特性就是模板了。模板給C++帶來了,編譯時的多態,通過模板元編程,C++可以實現類似C#,java的refection的特性。這裏我就舉來實現利用模板來代替虛函數。
 例子1:

#include <iostream>
using namespace std;
 
class common_base
{
public:
  virtual void fun()=0;
};
class common_derive:public common_base
{
public:
  void fun()
  { cout<<"in common_derive fun()"<<endl;
};
 
void main()
{
  common_base * pb = new common_derive;
  pb->fun();
}

  這是一個最普通的多態例子,下面看看一個比較有意思的例子:
例子2:

template<typename T>
class class1
{
public:
    class1(T t):m_val(t){}
    virtual T getVal(){
        cout<<"in class1,val =="<< m_val <<endl;
        return m_val;
    }
private:
    T m_val;
};

class derived: public class1<int>
{
public:
    derived(int i):class1<int>(i){}
    int getVal()
    {
        cout<<"in derived"<<endl;
        return class1<int>::getVal();
    }
};

template<typename T>
class derived2: public class1<T>
{
public:
    derived2(T val):class1<T>(val){}
    T getVal()
    {
        cout<<"in derived2"<<endl;
        return class1<T>::getVal();
    }
};

void main()
{
    class1<int> * pbase = new derived(10);
    pbase->getVal();

    class1<int> * pb2 = new derived2<int>(10);
    pb2->getVal();
}

這個例子我的用意是說明:模板類的虛函數多態,而且派生類可以有兩種選擇,一個實現爲普通類,繼承的是模板基類的特化類,一個就實現模板類(如 derived2)。很明顯模板繼承類有着普通類不可比擬的靈活性。

下面是這篇文章的重頭戲了,也是本文的目的所在。
我們看到例子1,2都採用虛函數來實現多態,這個是一般選擇,如何用模板來實現多態,而不是虛函數呢?
看這個例子:

template<class derive>
class base
{
public:
    void print()
    {
        derive::print();
    }
    void m_print()
    {
        downcast()->derive::m_print();
    }
protected:
    inline derive * downcast()
    {
        return static_cast<derive *>(this);
    };
    inline const derive * downcast()const
    {
        return static_cast<const derive *>(this);
    };
};

class der:public base<der>
{
public:
    der(int foo):_foo(foo){}
    static void print()
    {
        cout<<"in der print"<<endl;
    };
    void m_print()
    {
        cout<<"in der member fun m_print"<<endl;
        cout<<"has member foo="<<_foo<<endl;
    }
private:
    int _foo;
};

template<class base>
class der2:public base
{
public:
    static void print()
    {
        cout<<"in der2 print"<<endl;
    };
    void m_print()
    {
        cout<<"in der2 member fun m_print"<<endl;
    }
};

class tmpclass
{
public:
    void test()
    { cout<<"in test"<<endl;}
};

int main(int argc, char* argv[])
{
    //模板實現虛函數多態
    base<der> * pb= new der(100);
    pb->print();
    pb->m_print();

    //動態繼承
    der2<tmpclass> d2;
    d2.print();
    d2.m_print();
    d2.test();

    return 0;
}

哈哈,看class der是不是同樣實現了多態??而且語義和虛函數一致。可以進一步提取downcast()部分到一個基類實現更普遍的寫法。

後面我實現了一個動態繼承的類der2,它同樣提供了靈活的繼承用法,可惜好像因編譯器而定,在vc6環境下是不能通過編譯的,而在g++下是ok的。

 下面我編寫了一個性能測試例程來測試利用虛擬函數實現多態和模板實現多態的性能。代碼如下

#include <iostream>
using namespace std;
#include <sys/time.h>

class common_base
{
public:
    common_base(int iloop){_iloop=iloop;}
    virtual void virtual_fun()=0;
    void timesum_fun()
    {
        struct timeval begin,end;
        gettimeofday(&begin, NULL);
        for(int i=0;i<_iloop;i++)
            virtual_fun();
        gettimeofday(&end, NULL);
        cout<< "using time :" << end.tv_sec-begin.tv_sec + (end.tv_usec - begin.tv_usec)/1000000.0<<"  second"<<endl;
    };
private:
    int _iloop;
};
class common_derive:public common_base
{
public:
    common_derive(int iloop):common_base(iloop){_foo=0;}
    void virtual_fun()
    {
        ++_foo;
        --_foo;
    }
private:
    int _foo;
};

template<class derive>
class base
{
public:
    base(int iloop){_iloop=iloop;}
    void timesum_fun()
    {
        struct timeval begin,end;
        gettimeofday(&begin, NULL);
        for(int i=0;i<_iloop;i++)
            templ_fun();

        gettimeofday(&end, NULL);
        cout<< "using time :" << end.tv_sec-begin.tv_sec + (end.tv_usec - begin.tv_usec)/1000000.0<<"  second"<<endl;
    }
    inline void templ_fun()
    {
        downcast()->derive::templ_fun();
    }
protected:
    inline derive * downcast()
    {
        return static_cast<derive *>(this);
    };
    inline const derive * downcast()const
    {
        return static_cast<const derive *>(this);
    };
private:
    int _iloop;
};

class der:public base<der>
{
public:
    der(int iloop):base<der>(iloop){_foo=0;}
    inline void templ_fun()
    {
        ++_foo;
        --_foo;
    }
private:
    int _foo;
};

int main()
{
  int loop=1000*1000*100;
  common_base * pb = new common_derive(loop);
  base<der> * ptempb= new der(loop);
  for(int i =3;i-->0;)
  {
      cout<<"virtual function test: looptime="<<loop<<endl;
      pb->timesum_fun();
      cout<<"template function test: looptime="<<loop<<endl;
      ptempb->timesum_fun();
  }
  delete pb;
  delete ptempb;
  return 0;
}

我編譯了兩個版本一個優化版本一個未優化版本,運行測試結果讓我有點意外:

這是未優化版本的,結果顯示這兩種方法不相上下,虛函數還略優,~O~

./cmp_test
virtual function test: looptime=100000000
using time :1.03824  second
template function test: looptime=100000000
using time :1.63043  second
virtual function test: looptime=100000000
using time :1.03768  second
template function test: looptime=100000000
using time :1.62773  second
virtual function test: looptime=100000000
using time :1.63104  second

運行優化版本,性能優勢一下體現出來了,模板實現是虛函數的十倍:

./cmp_test_optimize 
virtual function test: looptime=100000000
using time :0.615542  second
template function test: looptime=100000000
using time :0.055584  second
virtual function test: looptime=100000000
using time :0.624778  second
template function test: looptime=100000000
using time :0.057419  second
virtual function test: looptime=100000000
using time :0.624977  second
template function test: looptime=100000000
using time :0.059442  second

有點驚人是不是?這個差別就是因爲虛函數是不可優化和內聯的,而模板函數是可內聯的,這個性能差異就很大,再次隨着虛表的增大虛函數的調用是有性能退化的,而這點對於模板函數來說是沒有的,因爲在編譯時,這一切都是靜態了。不過客觀的說,虛函數多態是C++語言內置的,在複雜度方面,應該首選虛函數,這裏提供的這個方法只是作者在學習過程中的一個體會,模板的世界實在是太奇妙和高深了。

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