- #include <cstdlib>
- using namespace std;
- class Normal {
- int _i;
- public:
- Normal() { _i = 0; }
- Normal(const Normal& nml) { _i = nml._i; }
- // other members omitted
- };
- class Polym {
- int *_pi;
- Polym(const Polym&); // forbids copy-constructor
- void setp(int* i) { _pi = i; }
- public:
- Polym() { _pi = 0; }
- Polym* clone();
- // other members omitted
- };
- //
- // part1 codes
- //
- //template<class T, bool isPolymorphic>
- //class NiftyContainer
- //{
- // // other members omitted
- //public:
- // void DoSomething()
- // {
- // T* pSomeObj = new T;
- // if (isPolymorphic) // branch1
- // {
- // T* pNewObj = pSomeObj->clone(); // customized behavior for copying
- // // some operations on polymorphic object
- // pNewObj = 0;
- // }
- // else // branch2
- // {
- // T* pNewObj = new T(*pSomeObj); // using copy-constructor
- // // some operations on non-polymorphic object
- // delete pNewObj;
- // }
- // delete pSomeObj;
- // }
- // // other members omitted
- //};
- //
- //int main(int argc, char* argv[])
- //{
- // // using the container with elements' type Normal
- // NiftyContainer<Normal, false> nc;
- // nc.DoSomething(); // error: 类Normal没有成员函数clone
- //
- // // using the container with elements' type Polym
- // NiftyContainer<Polym, true> nc2; // error: 类Polym的copy-constructor是private
- // nc2.DoSomething();
- //
- // system("PAUSE");
- // return EXIT_SUCCESS;
// 按照我们的预期,如果元素类型是多态的,那么只执行branch1部分,否则只执行branch2,
// 因为两种类型的元素有不同的拷贝函数。
// 但是编译器可不会理会这些,一律的都要加以编译,因此出错了。
//
// a 对于模板来说,不同的参数意味着不同的类型。
// b 此外,模板的编译有一个特性,
// 即没有用到的模板函数是不会参与到编译中的,只进行语法正确性的分析。
//
// c 函数可以根据参数进行重载。
//
// 为了做到“根据常数的值(此处就是isPolymorphic)只编译其中某一个分支
// (例如branch1),而不编译其它分支,从而保证最后的代码中只包含需要的代码”,
// 我们可以利用上述的模板编译特性和函数重载特性。
- // part2 codes
- //
- template<int v>
- struct Int2Type { // 创造了这个模板是为了用不同的常数值产生不同的类型
- enum {value=v};
- };
- template<class T, bool isPolymorphic>
- class NiftyContainer
- {
- private:
- void DoSomething(T* pObj, Int2Type<true>) // function_p
- {
- T* pNewObj = pObj->Clone();
- // some operations on polymorphic object
- pNewObj = 0;
- }
- void DoSomething(T* pObj, Int2Type<false>) // function_n
- {
- T* pNewObj = new T(*pObj);
- // some operations on non-polymorphic object
- }
- public:
- void DoSomething(T* pObj)
- {
- DoSomething(pObj, Int2Type<isPolymorphic>());
- }
- // other members are omitted
- };
- int main(int argc, char* argv[])
- {
- // using the container with elements' type Normal
- NiftyContainer<Normal, false> nc;// ok
- // using the container with elements' type Polym
- NiftyContainer<Polym, true> nc2; // ok
- system("PAUSE");
- return EXIT_SUCCESS;
- }
// 在编译过程中,根据传递的isPolymorphic模板参数值的不同,编译器将选择实现哪个函数。
// 若为true,则实现function_p,而不会尝试去编译function_n。
// 这样就成功做到了编译期多态,而不是执行期多态。
// 这个技术的关键点就在于Int2Type模板。
- <pre name="code" class="cpp">#include <iostream>
- #include <string>
- using namespace std;
- template<class T>
- struct Type2Type
- {
- typedef T OrgT;
- };
- class Widget
- {
- public:
- Widget(string str, int n)
- {
- cout<<str<<":"<<n<<endl;
- }
- };
- template<class T, class U>
- T *Create(const U &org, Type2Type<T>)
- {
- return new T(org);
- }
- template<class T>
- Widget *Create(const T&org, Type2Type<Widget>)
- {
- return new Widget(org, -1);
- }
- int main()
- {
- string *str = Create("hello", Type2Type<string>());
- cout<<*str<<endl;
- delete str;
- Widget *wid = Create("I am Widget", Type2Type<Widget>());
- delete wid;
- }
- ~
- </pre><br>
- <br>
- <pre></pre>
- <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>
- </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是跟编译器说:嘿,不要去管那个我不关心的分支。
妙啊!