So, I\'m following the example set by the code somewhere on this web page: http://eli.thegreenplace.net/2014/sfinae-and-enable_if/
Here\'s what I have:
t
The examples are wrong, since T is in a non-deduced context. Unless you call the function like fun, the code won't compile, but this is probably not what the author intended to show.
The correct usage would be to allow T to be deduced by the compiler, and to place a SFINAE condition elsewhere, e.g., in a return type syntax:
template
auto fun(const T& val)
-> typename std::enable_if::value>::type
{
std::cout << "fun";
}
template
auto fun(const T& val)
-> typename std::enable_if::value>::type
{
std::cout << "fun";
}
DEMO
Also, the typenames in your code contradict your usage of std::enable_if_t.
Use either c++11:
typename std::enable_if<...>::type
or c++14:
std::enable_if_t<...>
How would that work in a constructor which doesn't have a return type though?
In case of constructors, the SFINAE condition can be hidden in a template parameter list:
struct A
{
template ::value, int>::type = 0>
A(const T& val)
{
std::cout << "A";
}
template ::value, int>::type = 0>
A(const T& val)
{
std::cout << "A";
}
};
DEMO 2
Alternatively, in c++20, you can use concepts for that:
A(const std::integral auto& val);
A(const std::floating_point auto& val);