簡體   English   中英

函數模板c ++的顯式特化

[英]explicit specialization for function template c++

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;
}

這是函數模板,在調用validate函數時以某種方式在main ,即使參數為char ,它也從不使用第二個函數( const char& )。 任何人都可以看到我的問題在哪里?

你專門用的類型 - const char&你傳遞char時所推導的T不一致 - 它推斷為char

(模板類型參數只能在存在通用引用的情況下推斷為引用)

所以,

template <> 
bool validate<char> ...

無論如何,你為什么不超載呢?

暫無
暫無

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

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