type_have_function

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/artisans/article/details/84106694

#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
}

猜你喜欢

转载自blog.csdn.net/artisans/article/details/84106694
今日推荐