1

I'm trying to learn about SFINAE and came across a problem while trying to apply it to template class member functions. Thanks to Why doesn't SFINAE (enable_if) work for member functions of a class template? I was able to setup a basic example(Run online):

template<typename T>
class Foo{
    public:
    template <typename U = T, typename std::enable_if_t<!std::is_same<U, int>::value && !std::is_same<U, float>::value,int> = 0>
    void sfinae() { // Foo<anything else>
        std::cout << "sfinae default" << std::endl;
    }

    template <typename U = T, typename std::enable_if_t<std::is_same<U, int>::value,int> = 0>
    void sfinae() { // Foo<int>
        std::cout << "sfinae int" << std::endl;
    }

    template <typename U = T, typename std::enable_if_t<std::is_same<U, float>::value,int> = 0>
    void sfinae() { // Foo<float>
        std::cout << "sfinae float" << std::endl;
    }
};

I had two follow up questions to this:
1. How can I separate the declaration and definition of the member functions?
I tried something like below for the definition:

template <typename T>
template <typename U = T, typename std::enable_if_t<!std::is_same<U, int>::value && !std::is_same<U, float>::value,int> = 0>
    void Foo<T>::sfinae() { // Foo<anything else>
        std::cout << "sfinae default" << std::endl;
    }

But this gives a compilation error.

2. The catch-all default case(which prints sfinae-default) has to currently be written as not(type1, type2,...) which can potentially be huge. Is there a shorter/cleaner solution possible?

Edit: Based on the answer by @Jans, here is a corrected solution.

1 Answer 1

2

In out of line definition you remove the default arguments, they are taken from declarations:

template <class T>
template <typename U, typename std::enable_if_t<!std::is_same<U, int>::value && !std::is_same<U, float>::value,int>>
void Foo<T>::sfinae() { // Foo<anything else>
    std::cout << "sfinae default" << std::endl;
}

the same for the others

The catch-all default case(which prints sfinae-default) has to currently be written as not(type1, type2,...) which can potentially be huge. Is there a shorter/cleaner solution possible?

For this you need to add an extra parameter to rank the overloads giving the lowest rank to the catch-all one, this is usually done using the fact that the ellipsis parameter is not better than any other parameter type:

template <typename U = T>
void sfinae(...);

template <typename U = T, typename std::enable_if_t<std::is_same<U, int>::value,int> = 0>
void sfinae(int);

template <typename U = T, typename std::enable_if_t<std::is_same<U, float>::value,int> = 0>
void sfinae(int);

Foo<char>{}.sfinae(0); // select the catch-all

What this means is that since ... is worse than any other parameter (int is better than ...), the overload sfinae(...) is only considered if the other two can't be called, i.e if they SFINAE.

1
  • Thanks I can confirm this is working! That said: (1) Is it not possible without the extra "int" parameter? and (2) Could you add some details to your answer about how the ranking works exactly?
    – tangy
    Dec 11, 2018 at 5:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.