簡體   English   中英

模板部分專業化

[英]Template partial specialization

template<class T>
struct TypeX;

template<>
struct TypeX<int(...)>//HERE IF WITHOUT ELLIPSIS IT WILL COMPILE
{
    static std::string get_type()
    {
        return "int()";
    }
};

template<>
struct TypeX<int>
{
    static std::string get_type()
    {
        return "int";
    }
};

template<class T>
struct type_descriptor
{
    typedef T type;
    typedef typename std::remove_reference<T>::type no_ref_type;
    typedef typename std::remove_pointer<no_ref_type>::type no_ref_no_pointer_type;
    typedef typename std::remove_cv<no_ref_no_pointer_type>::type no_ref_no_pointer_no_cv_type;
    typedef typename std::remove_all_extents<no_ref_no_pointer_no_cv_type>::type no_ref_no_pointer_no_cv_no_ext_type;
    typedef no_ref_no_pointer_no_cv_no_ext_type bare_type;

    enum {isArray = std::is_array<T>::value, isPointer = std::is_pointer<T>::value, isRef = std::is_reference<T>::value};

    static std::string get_type()
    {
        return pointer_<isPointer>() + array_<std::is_array<no_ref_no_pointer_type>::value>() + TypeX<bare_type>::get_type();

    }
};
template<bool C>
std::string array_()
{return "";}

template<>
std::string array_<true>()
{return "array of";}

template<bool C>
std::string pointer_()
{return "";}

template<>
std::string pointer_<true>()
{return "pointer to";}

int _tmain(int argc, _TCHAR* argv[])
{
    cout << type_descriptor<int(*)()>::get_type();

    return 0;
}

請查看代碼中的注釋。 問題是為什么我是否專門研究省略號(假設任何數字我都會出錯),但是當我專門研究無參數時,它會編譯嗎?

問題是為什么我是否專門研究省略號(假設任何數字我都會出錯),但是當我專門研究無參數時,它會編譯嗎?

因為省略號並不意味着任何括號(因為您試圖在main使用它)。 省略號用於表示函數(C ++ 03)中可變數量的參數。


編輯:也許下面的示例為您提供了足夠的提示來實現您想要的:

template<class T>
struct TypeX
{
        TypeX() { cout << "TypeX" << endl; }
};

template<typename T>
struct TypeX<T(*)()> //will match with : int (*)(), char (*)(), etc!
{
        TypeX() { cout << "TypeX<T(*)()>" << endl; }
};

template<typename T, typename S>
struct TypeX<T(*)(S)> //will match with : int (*)(int), char (*)(int), etc!
{
        TypeX() { cout << "TypeX<T(*)(S)>" << endl; }
};

template<typename T, typename S, typename U>
struct TypeX<T(*)(S, U)> //will match with : int (*)(int, char), char (*)(int, int), etc!
{
       TypeX() { cout << "TypeX<T(*)(S, U)>" << endl; }
};
int main() {
        TypeX<int*>();
        TypeX<int(*)()>();
        TypeX<int(*)(int)>();
        TypeX<int(*)(char)>();
        TypeX<int(*)(char, int)>();
        TypeX<int(*)(short, char)>();
        return 0;
}

輸出:

TypeX
TypeX<T(*)()>
TypeX<T(*)(S)>
TypeX<T(*)(S)>
TypeX<T(*)(S, U)>
TypeX<T(*)(S, U)>

ideone上的演示: http : //www.ideone.com/fKxKK

省略號表示函數在每個調用位置可以接受任意數量的參數。

int f(...);  // signature

int x = f();   // invocations
int y = f(my_int, my_double);

函數簽名本身每次調用天真的隱含的更具體的簽名(即int f()int f(int, double) )不同。

因此,盡管您可以專門處理int (*)(...)情況,但只有實際指定初始省略號的函數類型將匹配。 其他函數,例如int (*)(int)不匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM