考虑以下系统:禁用默认模板,并且只使用专门通过SFINAE
template<typename T>
struct wrapper
{
operator T *() { return nullptr; }
};
template<typename Ret, typename T>
Ret func(T);
template<>
int func(float * in)
{
std::cout << "long";
}
template<>
long func(float * in)
{
std::cout << "int";
}
包装的目的是允许它衰减到它的模板的类型(它是围绕一个缓冲包装类型)。此外,我有一套功能模板的模板专业化。这是为了避免仅基于返回类型重载时的常见错误。
这不工作,虽然,如前所述这里:相反
// the following should work, but doesn't because it's instantiating
// the func<ret, wrapper<float>> which doesn't exist resulting in a linker error
// instead of selecting the int func(float *) overload
wrapper<float> w;
func<int>(w);
,我想这生成编译时错误(但同样,它生成一个链接时错误):
// the following should generate a compile-time error
// since no explicit overload for int func(int *) exists
wrapper<int> w2;
func<int>(w2);
所以理想情况下,我想禁用原始模板(如果可能的话可以通过sfinae?),这样重载决议只考虑明确的特化,并且如果没有找到匹配就会产生编译时错误。这可以做到吗?
铿锵和msvc之间的便携式解决方案是必须的,但我使用两者的最新版本。
您的问题,还不如说是模板参数推导,不考虑隐式转换尽可能多的重载决议。 – 2014-10-06 16:04:02
@ P0W因为你不能超载这两个。唯一的区别是返回类型。 – 2014-10-06 16:14:17
@TC Ahh我看到了,谢谢 – P0W 2014-10-06 16:15:25