我想知道是否可以解决这个模糊的模板函数:
//function1
template<typename returnType>
returnType call()
{
//function with return type
}
//function2
template<typename var>
void call()
{
//function without return type
}
call<int>(); //call function1
call<void>(); //call function2
我想阻止以下解决方案:
//function1
template<typename returnType>
returnType call()
{
//function with return type
}
//function2
void call()
{
//function without
}
call<int>(); //call function1
call(); //call function2
答案 0 :(得分:5)
您可以明确专门化void
的模板:
//function2
template<>
void call<void>()
{
//function without return type
}
答案 1 :(得分:0)
我不确定我是否明白这一点,但我尝试了SFINAE:
//function1
template<typename returnType>
typename std::enable_if
<
!std::is_same< returnType, void >::value,
returnType
>::type call()
{
//function with return type
}
//function2
template<typename var>
typename std::enable_if
<
std::is_same< var, void >::value
>::type call()
{
//function without return type
}