繁体   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