3
template <typename T> 
bool validate(const T& minimum, const T& maximum, const T& testValue) { 
    return testValue >= minimum && testValue <= maximum; 
} 

template <> 
bool validate<const char&>(
    const char& minimum, 
    const char& maximum, 
    const char& testValue) 
{ 
    char a = toupper(testValue); 
    char b = toupper(minimum); 
    char c = toupper(maximum); 
    return a >= b && a <= c; 
} 

這明確的專業化是函數模板,不知何故在mainvalidate函數被調用,它絕不會使用第二個功能(一個用於const char&),即使參數char。任何人都可以看到我的問題在哪裏?爲函數模板C++

+0

告訴我們你是怎麼稱呼它的。 – user975989

回答

6

你專門用於該類型 - const char&不匹配T推斷,當你傳遞一個char因爲 - 它演繹到char

所以,

template <> 
bool validate<char> ... 

不管怎麼說,你爲什麼不超載,而不是(模板類型參數只能在普遍引用的存在演繹到引用)?