Home Specialize template which includes enableif
Reply: 3

Specialize template which includes enableif

PaulProgrammerNoob
1#
PaulProgrammerNoob Published in 2018-01-12 22:56:00Z

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.

Jarod42
2#
Jarod42 Reply to 2018-01-12 23:35:16Z

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())

SoronelHaetir
3#
SoronelHaetir Reply to 2018-01-12 23:01:50Z

In:

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

Gid rid tof typename before void.

StPiere
4#
StPiere Reply to 2018-01-12 23:25:26Z

I think at some point in the recursion you are reaching the place where no template can be instantiated. Try to declare the general template at the top which breaks the recursion first and then do the rest. There is some weird logic that I don't quite understand but I would try this at least to compile. I think that enable_if is not in the right place in your case. Would tipp at some design issues in general.

template<typename T>
void onlyForFancyGuys(T* t) {
}

and

 template<typename T, bool b>
    void onlyForFancyGuys(T* t) {
    }

And remove typename before void in the specialization, like somebody said.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.350361 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO