type_have_function
阿新 • • 發佈:2018-12-18
#ifndef type_have_function
#define type_have_function(NAME, FUNC)\
template<typename T>\
struct NAME \
{\
private:\
template<typename>\
struct check : std::true_type {};\
\
template<typename C> static auto test(int)->\
check<decltype(::std::declval<C>().FUNC )>;\
\
template<class>\
static auto test(long)->std::false_type;\
\
template<typename C>\
struct verify : decltype(test<C>(0)){};\
\
public:\
static constexpr bool value{ verify<T>() };\
};
#endif
struct AA
{
void Update(float) {}
};
struct BB
{
void f() {}
};
struct CC
{
void f2(int a, int b){}
};
type_have_function(have_update, Update(0.0f))
type_have_function(have_f, f())
type_have_function(have_f2, f2(0, 0))
int main(int argc, char** argv)
{
bool b1 = have_update<AA> ::value;//true
bool b2 = have_f<BB>::value;//true
bool b3 = have_f<CC>::value; //false
bool b4 = have_f2<CC>::value;//true
}