#include <type_traits>
template < typename T, typename TMethod>
class has_bar_method
{
private :
struct yes { char _; } ;
struct no { char _[ 2 ] ; } ;
template < typename U, TMethod = & U:: Bar >
static yes impl( U* ) ;
static no impl( ...) ;
public :
enum { value = sizeof ( impl( static_cast < T* > ( nullptr) ) ) == sizeof ( yes) } ;
} ;
template < class T>
class A
{
private :
public :
void Foo( T& b)
{
static_assert( has_bar_method< T, void ( T:: * ) ( float ) > :: value , "T has method void Bar(float)" ) ;
b.Bar ( 0.5 ) ;
}
} ;
class B
{
public :
//void Bar(float) {}
void Bar( double ) { }
void Bar( int ) { }
} ;
int main( ) {
A< B> a;
B b;
a.Foo ( b) ;
return 0 ;
}
I2luY2x1ZGUgPHR5cGVfdHJhaXRzPgoKdGVtcGxhdGUgPHR5cGVuYW1lIFQsIHR5cGVuYW1lIFRNZXRob2Q+CmNsYXNzIGhhc19iYXJfbWV0aG9kCnsKcHJpdmF0ZToKCXN0cnVjdCB5ZXMgeyBjaGFyIF87IH07CglzdHJ1Y3Qgbm8geyBjaGFyIF9bMl07IH07Cgl0ZW1wbGF0ZSA8dHlwZW5hbWUgVSwgVE1ldGhvZCA9ICZVOjpCYXI+CglzdGF0aWMgeWVzIGltcGwoVSopOwoJc3RhdGljIG5vIGltcGwoLi4uKTsKCQpwdWJsaWM6CgllbnVtIHsgdmFsdWUgPSBzaXplb2YoaW1wbChzdGF0aWNfY2FzdDxUKj4obnVsbHB0cikpKSA9PSBzaXplb2YoeWVzKSB9Owp9OwoKdGVtcGxhdGUgPGNsYXNzIFQ+CmNsYXNzIEEKewpwcml2YXRlOgoJCnB1YmxpYzoKICAgdm9pZCBGb28oVCYgYikKICAgewogICAJICAgc3RhdGljX2Fzc2VydChoYXNfYmFyX21ldGhvZDxULCB2b2lkIChUOjoqKShmbG9hdCk+Ojp2YWx1ZSwgIlQgaGFzIG1ldGhvZCB2b2lkIEJhcihmbG9hdCkiKTsKIAogICAgICAgYi5CYXIoMC41KTsKICAgfQp9OwogCmNsYXNzIEIKewpwdWJsaWM6CiAgICAvL3ZvaWQgQmFyKGZsb2F0KSB7fQogICAgdm9pZCBCYXIoZG91YmxlKSB7fQogICAgdm9pZCBCYXIoaW50KSB7fQp9OwoKaW50IG1haW4oKSB7CglBPEI+IGE7CglCIGI7CgkKCWEuRm9vKGIpOwoJCglyZXR1cm4gMDsKfQ==
compilation info
prog.cpp: In instantiation of ‘void A<T>::Foo(T&) [with T = B]’:
prog.cpp:43:9: required from here
prog.cpp:25:8: error: static assertion failed: T has method void Bar(float)
static_assert(has_bar_method<T, void (T::*)(float)>::value, "T has method void Bar(float)");
^
stdout