模板参数推导失败,SFINAE
Template argument deduction failed, SFINAE
当我编译这段代码时:
#include <type_traits>
template <typename T>
void do_stuff(std::enable_if_t<std::is_integral<T>::value, T> &t) {}
template <typename T>
void do_stuff(std::enable_if_t<std::is_class<T>::value, T> &t) {}
int main() {
int i = 1;
do_stuff(i);
return 0;
}
海湾合作委员会说:
37325975.cpp: In function ‘int main()’:
37325975.cpp:11:15: error: no matching function for call to ‘do_stuff(int&)’
do_stuff(i);
^
37325975.cpp:4:6: note: candidate: template<class T> void do_stuff(std::enable_if_t<std::is_integral<_Tp>::value, T>&)
void do_stuff(std::enable_if_t<std::is_integral<T>::value, T> &t) {}
^
37325975.cpp:4:6: note: template argument deduction/substitution failed:
37325975.cpp:11:15: note: couldn't deduce template parameter ‘T’
do_stuff(i);
^
37325975.cpp:7:6: note: candidate: template<class T> void do_stuff(std::enable_if_t<std::is_class<T>::value, T>&)
void do_stuff(std::enable_if_t<std::is_class<T>::value, T> &t) {}
^
37325975.cpp:7:6: note: template argument deduction/substitution failed:
37325975.cpp:11:15: note: couldn't deduce template parameter ‘T’
do_stuff(i);
^
我也试过 msvc 2013。
为什么会出现这些错误?
正如编译器所说,该参数类型是不可推导的,因此您需要手动提供模板参数,如下所示:
do_stuff<int>(i);
更好的选择是将 std::enable_if
放在 return 类型或模板参数列表中:
//Return type
template <typename T>
std::enable_if_t<std::is_integral<T>::value>
do_stuff(T &t) {}
template <typename T>
std::enable_if_t<std::is_class<T>::value>
do_stuff(T &t) {}
//Parameter list
template <typename T, std::enable_if_t<std::is_integral<T>::value>* = nullptr>
void do_stuff(T &t) {}
template <typename T, std::enable_if_t<std::is_class<T>::value>* = nullptr >
void do_stuff(T &t) {}
这样仍然可以推导出模板参数:
do_stuff(i);
Why do I get these errors?
因为嵌套名称说明符的模板参数推导失败,即 non-deduced contexts。
The nested-name-specifier (everything to the left of the scope resolution operator ::) of a type that was specified using a qualified-id.
// the identity template, often used to exclude specific arguments from deduction
template<typename T> struct identity { typedef T type; };
template<typename T> void bad(std::vector<T> x, T value = 1);
template<typename T> void good(std::vector<T> x, typename identity<T>::type value = 1);
std::vector<std::complex<double>> x;
bad(x, 1.2); // P1 = std::vector<T>, A1 = std::vector<std::complex<double>>
// P1/A1: deduced T = std::complex<double>
// P2 = T, A2 = double
// P2/A2: deduced T = double
// error: deduction fails, T is ambiguous
good(x, 1.2); // P1 = std::vector<T>, A1 = std::vector<std::complex<double>>
// P1/A1: deduced T = std::complex<double>
// P2 = identity<T>::type, A2 = double
// P2/A2: uses T deduced by P1/A1 because T is to the left of :: in P2
// OK: T = std::complex<double>
当编译器尝试解析 do_stuff(int&)
时,它会看到编译器告诉您的两个候选项。但是无法"work backwards"找到满足std::enable_if_t<std::is_integral<T>::value, T> == int
的T
,也找不到满足std::enable_if_t<std::is_class<T>::value, T> == int
.
的T
如TartanLlama's answer中所述,避免这种情况的方法是使参数可推导(例如do_stuff(T&)
)并使return类型 或 后续模板参数 依赖于 T
.
当我编译这段代码时:
#include <type_traits>
template <typename T>
void do_stuff(std::enable_if_t<std::is_integral<T>::value, T> &t) {}
template <typename T>
void do_stuff(std::enable_if_t<std::is_class<T>::value, T> &t) {}
int main() {
int i = 1;
do_stuff(i);
return 0;
}
海湾合作委员会说:
37325975.cpp: In function ‘int main()’:
37325975.cpp:11:15: error: no matching function for call to ‘do_stuff(int&)’
do_stuff(i);
^
37325975.cpp:4:6: note: candidate: template<class T> void do_stuff(std::enable_if_t<std::is_integral<_Tp>::value, T>&)
void do_stuff(std::enable_if_t<std::is_integral<T>::value, T> &t) {}
^
37325975.cpp:4:6: note: template argument deduction/substitution failed:
37325975.cpp:11:15: note: couldn't deduce template parameter ‘T’
do_stuff(i);
^
37325975.cpp:7:6: note: candidate: template<class T> void do_stuff(std::enable_if_t<std::is_class<T>::value, T>&)
void do_stuff(std::enable_if_t<std::is_class<T>::value, T> &t) {}
^
37325975.cpp:7:6: note: template argument deduction/substitution failed:
37325975.cpp:11:15: note: couldn't deduce template parameter ‘T’
do_stuff(i);
^
我也试过 msvc 2013。
为什么会出现这些错误?
正如编译器所说,该参数类型是不可推导的,因此您需要手动提供模板参数,如下所示:
do_stuff<int>(i);
更好的选择是将 std::enable_if
放在 return 类型或模板参数列表中:
//Return type
template <typename T>
std::enable_if_t<std::is_integral<T>::value>
do_stuff(T &t) {}
template <typename T>
std::enable_if_t<std::is_class<T>::value>
do_stuff(T &t) {}
//Parameter list
template <typename T, std::enable_if_t<std::is_integral<T>::value>* = nullptr>
void do_stuff(T &t) {}
template <typename T, std::enable_if_t<std::is_class<T>::value>* = nullptr >
void do_stuff(T &t) {}
这样仍然可以推导出模板参数:
do_stuff(i);
Why do I get these errors?
因为嵌套名称说明符的模板参数推导失败,即 non-deduced contexts。
The nested-name-specifier (everything to the left of the scope resolution operator ::) of a type that was specified using a qualified-id.
// the identity template, often used to exclude specific arguments from deduction template<typename T> struct identity { typedef T type; }; template<typename T> void bad(std::vector<T> x, T value = 1); template<typename T> void good(std::vector<T> x, typename identity<T>::type value = 1); std::vector<std::complex<double>> x; bad(x, 1.2); // P1 = std::vector<T>, A1 = std::vector<std::complex<double>> // P1/A1: deduced T = std::complex<double> // P2 = T, A2 = double // P2/A2: deduced T = double // error: deduction fails, T is ambiguous good(x, 1.2); // P1 = std::vector<T>, A1 = std::vector<std::complex<double>> // P1/A1: deduced T = std::complex<double> // P2 = identity<T>::type, A2 = double // P2/A2: uses T deduced by P1/A1 because T is to the left of :: in P2 // OK: T = std::complex<double>
当编译器尝试解析 do_stuff(int&)
时,它会看到编译器告诉您的两个候选项。但是无法"work backwards"找到满足std::enable_if_t<std::is_integral<T>::value, T> == int
的T
,也找不到满足std::enable_if_t<std::is_class<T>::value, T> == int
.
T
如TartanLlama's answer中所述,避免这种情况的方法是使参数可推导(例如do_stuff(T&)
)并使return类型 或 后续模板参数 依赖于 T
.