I used SFINAE many times successfully. To detect if a class provides a function is not a problem. My current problem seems to be the opposite of his problem! Instead of also detect the derived methods, I would prefer to detect only the class' methods. It seems to be related to the fact that the method is a template.
Is it possible to detect a class template method? I tried to instantiate the template with a type which shouldn't harm, but no luck.
struct A { template<class T> void Func( T ) {}; };
struct B :A {};
template< class T >
struct CheckForFunc
{
typedef char(&YesType)[1];
typedef char(&NoType)[2];
template< class U, void (U::*)( int ) > struct Sfinae;
template< class T2 > static YesType Test( Sfinae<T2,&T2::Func>* );
template< class T2 > static NoType Test( ... );
static const bool value = sizeof(Test<T>(0))==sizeof(YesType);
};
int main(int argc, char* argv[])
{
// gives "1"
std::cout << "Value A=" << CheckForFunc< A >::value << std::endl;
// doesn't compile!
std::cout << "Value B=" << CheckForFunc< B >::value << std::endl;
return 0;
}
Error message:
error: ‘&A::Func’ is not a valid template argument for type ‘void (B::*)(int)’ because it is of type ‘void (A::*)(int)’
Note that this SFINAE works very well with template methods, just not with derivation! Bad is that it doesn't just detect wrong, it fails compilation.
How to write a SFINAE test without using a 'sample' type (here: the int)?
Edit: Sorry, C++03 only! And LLVM was fine with it, also VS2008, just not GCC and QNX (version I would have to look tomorrow).
Edit2: Didn't know about Coliru! Very cool, here is the error!