“已经定义了功能模板".与互斥的`enable_if`s [英] "Function template has already been defined" with mutually exclusive `enable_if`s
本文介绍了“已经定义了功能模板".与互斥的`enable_if`s的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
MSVC为以下代码产生错误(功能模板已定义"):
MSVC produces error ("function template has already been defined") for the following code:
template<typename T, typename = std::enable_if_t<std::is_default_constructible<T>::value>>
auto foo(T&& val) {
return 0;
}
// note difference from above ---> !
template<typename T, typename = std::enable_if_t<!std::is_default_constructible<T>::value>>
auto foo(T&& val) {
return 0;
}
我认为这会起作用,因为存在互斥的sfinae条件.您能帮助我加深我的理解力吗?
I thought it would work because there are mutually exclusive sfinae conditions. Can you help me with the hole in my understanding?
推荐答案
是的,它们的签名是相同的;默认模板参数不是功能模板签名的一部分.
Yes, their signature are the same; the default template arguments are not the part of the function template signature.
您可以将它们更改为
// the 2nd non-type template parameter are different
template<typename T, std::enable_if_t<std::is_default_constructible<T>::value>* = nullptr>
auto foo(T&& val) {
return 0;
}
template<typename T, std::enable_if_t<!std::is_default_constructible<T>::value>* = nullptr>
auto foo(T&& val) {
return 0;
}
或
// the return type are different
template<typename T>
std::enable_if_t<std::is_default_constructible<T>::value, int> foo(T&& val) {
return 0;
}
template<typename T>
std::enable_if_t<!std::is_default_constructible<T>::value, int> foo(T&& val) {
return 0;
}
这篇关于“已经定义了功能模板".与互斥的`enable_if`s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文