工作在返回类型但不作为模板参数的SFINAE
std::enable_if<>
int main(){
foo(5);
foo(3.4);}foo
template<typename T,
typename = typename std::enable_if<std::is_integral<T>::value>::type>auto foo(T)
-> void{
std::cout << "I'm an integer!\n";}template<typename T,
typename = typename std::enable_if<std::is_floating_point<T>::value>::type>auto foo(T)
-> void{
std::cout << "I'm a floating point number!\n";}template<typename T>auto foo(T)
-> typename std::enable_if<std::is_integral<T>::value>::type{
std::cout << "I'm an integrer!\n";}template<typename T>auto foo(T)
-> typename std::enable_if<std::is_floating_point<T>::value>::type{
std::cout << "I'm a floating point number!\n";}foo
main.cpp:14:6: error: redefinition of 'template<class T, class> void foo(T)' auto foo(T) ^main.cpp:6:6: note: 'template<class T, class> void foo(T)' previously declared here auto foo(T) ^main.cpp: In function 'int main()':main.cpp:23:12: error: no matching function for call to 'foo(double)' foo(3.4); ^main.cpp:6:6: note: candidate: template<class T, class> void foo(T) auto foo(T) ^main.cpp:6:6: note: template argument deduction/substitution failed:main.cpp:5:10: error: no type named 'type' in 'struct std::enable_if<false, void>' typename = typename std::enable_if<std::is_integral<T>::value>::type> ^
编辑 :
哆啦的时光机
喵喔喔
千万里不及你
随时随地看视频慕课网APP
相关分类