This question already has an answer here:
I have a class template Bird
with a Boolean template parameter can_fly
. Depending on that value, I want to enable a member function with the signature void fly();
.
This is my code:
#include <type_traits>
template<bool can_fly>
class Bird {
public:
template<typename void_t = typename std::enable_if<can_fly>::type>
void_t fly() { /* ... */ }
};
int main() {
Bird<true> flyingBird;
flyingBird.fly();
Bird<false> flightlessBird;
return 0;
}
This code compiles fine in Visual Studio 2015, but GCC complains that there is "no type named 'type' in 'struct std::enable_if'" in the third line of main
.
I thought the fact that there is no ::type
in the false
case was the entire point of SFINAE. Can somebody explain to me what I did wrong and what the correct approach is?
As mentioned in this answer:
In your case there is no substitution because
can_fly
is known at the moment of instantiation. You can create a dummy defaultbool
template parameter to make SFINAE work properly:wandbox example