点评:通过函数重载实现

  方法4:

template<typename T>
struct IsString
{
    enum { value = false };
};
template<>
struct IsString<string>
{
    enum { value = true };
};
template<typename T1, typename T2>
struct Base
{
    //other function
    //....
    void Func()
    {
        if(IsString<T2>::value)
        {
            cout << "specialization function" << endl;
        }
        else
        {
            cout << "primary function" << endl;
        }
    }
};

  点评:通过编译时类型判断实现。

  方法5:

template<typename T3,  typename T4>
struct must_be_same_type
{
    enum { ret = 0 };
};
template<>
struct must_be_same_type<string, string>
{
    enum { ret = 1 };
};
template < typename T1,typename T2 >
class Base{
public:
    //other function
    //....
    void Func(){
        if(must_be_same_type<T2, string>::ret)
        {
            cout << "specialization function" << endl;
        }
        else
        {
            cout << "primary function" << endl;
        }
    }
};

  点评:和方法4类似,是不过实现方式不一样。

  后,探讨下我自己遇到的问题,我们在写一个事件委托(delegate)类,大概如下:

template<typename return_type, typename first_type, typename second_type>
class CEvent
{
public:
    //other function
    //....
    return_type operator()(first_type p1, second_type p2)
    {
        return_type ret = return_type();
        //...
        //ret = invoker(p1, p2);
        return ret;
    }
};
void test3()
{
    CEvent<int, int, int> e1;
    e1(1, 2);
    CEvent<void, int, int> e2;
    e2(1, 2);
}
int main()
{
    test3();
}