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;
}
請參閱代碼評論。問題是爲什麼如果我專門處理省略號,這意味着任何數字我得到一個錯誤,但是當我專門爲沒有參數編譯?模板部分專業
我不明白,你能解釋一下嗎?在主要我試圖使用專精:指向fnc零參數和返回int。 – 2011-03-08 21:12:35
@我們無能爲力:實際上,在專業化中不允許使用''語法。你必須這樣做:http://www.ideone.com/ah1iH ........讓我知道如果你有任何進一步的問題! –
Nawaz
2011-03-10 07:31:11
@我們無能爲力:我編輯了我的答案。請讓我知道它對您有多大幫助...以及更多示例:http://www.ideone.com/fKxKK – Nawaz 2011-03-10 08:13:06