This question already has an answer here:
Very often I used SFINAE before but I have a very very simple example I can't get to run today.
class X
{
public:
template <typename CHECK, typename = typename std::enable_if< std::is_floating_point<CHECK>::value, void>::type >
void Do()
{
std::cout << "yes" << std::endl;
}
template <typename CHECK, typename = typename std::enable_if< !std::is_floating_point<CHECK>::value, void>::type>
void Do()
{
std::cout<< "no" << std::endl;
}
};
int main()
{
X x;
x.Do<float>();
}
Error:
src/main.cpp:20:18: error: 'template void X::Do()' cannot be overloaded
src/main.cpp:14:18: error: with 'template void X::Do()' void Do()
I want to disable any overload with enable_if but it doesn't work...
Any idea what today I did wrong?
Another syntax which compiles and works is to move the
enable_is
as the return type:The two functions have the same sigature, so you get a redefinition error. Try it with the following instead, which uses default arguments:
DEMO
See also the answers here and here.