'Why enable_if_t needs to have datatype identifier and a default value?
I am unable to understand how the 2 commented code lines in below snippet are different than the lines just ahead of them? Is there an easy way to understand the meaning of the commented lines vs the meaning of lines just ahead of them? I am unable to speak in my mind as how to read the commented line and the line next to it. Could anyone please explain? And don't point me to the documentation please. I have spent time there and still its not clear to me else i wouldn't be posting this question here.
#include <iostream>
#include <type_traits>
//template<class T, std::enable_if_t<std::is_integral_v<T>, bool>>
template<class T, std::enable_if_t<std::is_integral_v<T>, bool> K = true>
void fun(T value)
{
std::cout << "\n In Integral version";
}
//template<class T, std::enable_if_t<std::is_floating_point_v<T>, bool>>
template<class T, std::enable_if_t<std::is_floating_point_v<T>, bool> K = true>
void fun(T value)
{
std::cout << "\n In Floating point version";
}
int main()
{
fun(4);
fun(4.4);
}
following errors are shown when i use commented code and i dont know what they mean or how the above code resolves them.
Error(s):
2044199993/source.cpp: In function ‘int main()’:
2044199993/source.cpp:22:10: error: no matching function for call to ‘fun(int)’
fun(4);
^
2044199993/source.cpp:8:6: note: candidate: template<class T, typename std::enable_if<is_integral_v<T>, bool>::type <anonymous> > void fun(T)
void fun(T value)
^~~
2044199993/source.cpp:8:6: note: template argument deduction/substitution failed:
2044199993/source.cpp:22:10: note: couldn't deduce template parameter ‘<anonymous>’
fun(4);
^
2044199993/source.cpp:15:6: note: candidate: template<class T, typename std::enable_if<is_floating_point_v<T>, bool>::type <anonymous> > void fun(T)
void fun(T value)
^~~
2044199993/source.cpp:15:6: note: template argument deduction/substitution failed:
2044199993/source.cpp:22:10: note: couldn't deduce template parameter ‘<anonymous>’
fun(4);
^
2044199993/source.cpp:23:12: error: no matching function for call to ‘fun(double)’
fun(4.4);
^
2044199993/source.cpp:8:6: note: candidate: template<class T, typename std::enable_if<is_integral_v<T>, bool>::type <anonymous> > void fun(T)
void fun(T value)
^~~
2044199993/source.cpp:8:6: note: template argument deduction/substitution failed:
2044199993/source.cpp:23:12: note: couldn't deduce template parameter ‘<anonymous>’
fun(4.4);
^
2044199993/source.cpp:15:6: note: candidate: template<class T, typename std::enable_if<is_floating_point_v<T>, bool>::type <anonymous> > void fun(T)
void fun(T value)
^~~
2044199993/source.cpp:15:6: note: template argument deduction/substitution failed:
2044199993/source.cpp:23:12: note: couldn't deduce template parameter ‘<anonymous>’
fun(4.4);
^
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
