【发布时间】:2026-01-10 04:55:01
【问题描述】:
我想知道是否可以解决这个模棱两可的模板函数:
//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
【问题讨论】:
-
使用特化而不是重载
-
为什么没有两个函数呢?你不能多态地使用它们,而且它们在语义上可能完全不同