編譯期判斷類型之間是否可以convert

//T could converted to U ?
template<typename T, typename U>
class Conversion
{
private:
    typedef char Small;
    struct Big{ char big[2]; };

    static Small _helper_fun(U);
    static Big _helper_fun(...);
    static T _make_T();
public:
    enum {
        Exists = (sizeof(_helper_fun(_make_T())) == sizeof(Small)),
        Exists2Way = ( Exists && Conversion<U,T>::Exists),
        Same = false
    };
};

// partial specialization for "same type"
template<typename T>
class Conversion<T, T>
{
public:
    enum{
    Exists = true,
    Exists2Way = true,
    Same = true
    };
};

// T is subclass of U ?
template<typename T, typename U>
class IsSubclass
{
public:
    enum{
    Result = Conversion<T*, U*>::Exists
    };
};

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