2011-03-08 115 views
0
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; 
} 

請參閱代碼評論。問題是爲什麼如果我專門處理省略號,這意味着任何數字我得到一個錯誤,但是當我專門爲沒有參數編譯?模板部分專業

回答

1

問題是爲什麼,如果我專門爲 省略號,它假設意味着 任意數量的我發現了一個錯誤,但 當我專門爲沒有參數它 編譯?

因爲省略號並不意味着任何數量的括號(如你想在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

+0

我不明白,你能解釋一下嗎?在主要我試圖使用專精:指向fnc零參數和返回int。 – 2011-03-08 21:12:35

+0

@我們無能爲力:實際上,在專業化中不允許使用''語法。你必須這樣做:http://www.ideone.com/ah1iH ........讓我知道如果你有任何進一步的問題! – Nawaz 2011-03-10 07:31:11

+0

@我們無能爲力:我編輯了我的答案。請讓我知道它對您有多大幫助...以及更多示例:http://www.ideone.com/fKxKK – Nawaz 2011-03-10 08:13:06

0

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

int f(...); // signature 

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

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

因此,儘管你可以專門int (*)(...)情況下,只有一個函數類型,實際上規定了初始的省略號將匹配。其他功能如int (*)(int)不匹配。