2011-03-08 4 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; 
} 

출력 : http://www.ideone.com/fKxKK

+0

설명 할 수 없습니까? main에서 저는 specialization을 사용하려고합니다 : fnc에 대한 포인터는 args를 취하지 않고 int를 반환합니다. –

+0

@ 우리가 할 수있는 일은 없습니다 : 실제로 ''구문은 전문화되지 않습니다. 당신은 이것을해야합니다 : http://www.ideone.com/ah1iH ........ 더 이상의 질문이 있으면 알려주세요! – Nawaz

+0

@ 우리가 할 수있는 일은 아무것도 없다. 나는 나의 대답을 편집했다. 더 많은 도움이 될만한 것이 있다면 ... http://www.ideone.com/fKxKK – Nawaz

0

: ideone에서

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

데모 elipsis는 함수가 호출의 각 위치에서 인수의 수를 허용 할 수 있음을 의미합니다.

int f(...); // signature 

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

함수 서명 자체 소박 각 호출에 의미를 더 특정 시그니처 구별 (즉 int f(), int f(int, double))을한다.

따라서 int (*)(...) 사례를 전문으로하는 반면 실제로 실제로 초기 elipsis를 지정하는 함수 유형 만 일치합니다. int (*)(int)과 같은 다른 기능은 일치하지 않습니다.

관련 문제