Search code examples
c++templatestemplate-specializationenable-if

Specialize template which includes enableif


I want to specialize a templated method. It makes use of std::enable_if to check a static property of the given type:

template <class T, bool fancy= T::IsFancy()>
typename std::enable_if<fancy, void>::type
onlyForFancyGuys(T* component) {
    /*stuff*/
    onlyForFancyGuys(component->parent);
}

As I use this for recursive calls, I need a way to determine, when recursion ends. That is, when type Foo is used. So I tried this specialization.

template<>
typename void onlyForFancyGuys<Foo, true>(Foo* component);

and

template<>
void onlyForFancyGuys<Foo, true>(Foo* component);

But it keeps telling, me that this template-id does not match any template declaration. What am I doing wrong here? Is there something specific with enable_if?

Important fact: Foo does not have the method IsFancy.

Edit: I added IsFancy to Foo, but it does not make any difference.

Edit: I am compiling with MinGW. But I plan to use MSVC, too.

Edit: Adding IsFancy to Foo together with the accepted answer did the trick.


Solution

  • Just use overload:

    void onlyForFancyGuys(Foo* component) { /* ... */ }
    
    template <class T, bool fancy = T::IsFancy()>
    typename std::enable_if<fancy, void>::type
    onlyForFancyGuys(T* component) {
        /*stuff*/
        onlyForFancyGuys(component->parent);
    }
    

    template will be exclude thank to SFINAE (on T::IsFancy())