There are 2 SFINAE snippets that I coded.
They do exactly the same thing.
However, the first one works, while the second doesn't.
Why? (The second is more similar to my real program.)
This code works ( http://coliru.stacked-crooked.com/a/50e07af54708f076 )
#include <iostream>
#include <type_traits>
enum EN{ EN1,EN2 };
template<EN T1> class B{
public: template<EN enLocal=T1> typename
std::enable_if<enLocal==EN1,void>::type test(){ std::cout<<"1"<<std::endl;}
public: template<EN enLocal=T1> typename
std::enable_if<enLocal==EN2,void>::type test(){ std::cout<<"2"<<std::endl; }
};
int main(){
B<EN1> b;
b.test();
}
But this code is uncompilable (http://coliru.stacked-crooked.com/a/28b6afd443b36c7e) :-
#include <iostream>
#include <type_traits>
enum EN{ EN1,EN2 };
class Base{
public: void test(){
std::cout<<"1"<<std::endl;
};
};
template<EN T1> class B : public Base{
public: template<EN enLocal=T1>
std::enable_if_t< enLocal==EN2,void > test(){
std::cout<<"2"<<std::endl;
}
};
int main(){
B<EN1> bEn1; bEn1.test(); //should print 1
//B<EN2> bEn2; bEn2.test(); //print 2
}
I am very new to SFINAE and still learning it via https://stackoverflow.com/a/50562202/.