using namespace boost;
using namespace std;
class TestA{
public:
int* foo(){
int* a =new int(1);
return a;
}
static int foo2(){
return 1;
}
static const int foo3;
};
HAS_MEMBER_FUNC_CHECKER(foo);
HAS_MEMBER_FUNC_CHECKER(foo2);
HAS_MEMBER_FUNC_CHECKER(foo3);
int main() {
static_assert(HAS_MEMBER_FUNC(TestA, foo));
std::cout<<HAS_MEMBER_FUNC(TestA, foo) <<std::endl;
return 0;
}
对于确定参数类型的泛型成员函数:
using namespace boost;
using namespace std;
// 对于固定个数的模版类,检测某一个属性或者函数是否存在
// #define IF_HAS_MEMBER_FUNC2(FUNC,CLASS_,CLASS_F_T) (HAS_MEMBER_FUNC_CEHCK_NAME2_(FUNC)<CLASS_ ,CLASS_F_T>::value)
class TestA{
public:
int* foo(){
int* a =new int(1);
return a;
}
static int foo2(){
return 1;
}
template <typename T>
static int foo4(){
return 1;
}
static const int foo3;
};
//含有固定类型的模版类
HAS_MEMBER_FUNC_CEHCKER2(foo4);
int main() {
// 输出 1
std::cout<< HAS_MEMBER_FUNC_CHECK_NAME2_(foo4)<TestA,char>::value << std::endl;
// 输出 1
std::cout<< HAS_MEMBER_FUNC_CHECK_NAME2_(foo4)<TestA,int>::value << std::endl;
return 0;
}
See also:
https://blog.csdn.net/TH_NUM/article/details/90968219
https://harrychen.xyz/2019/06/04/cpp-17-mock-concept/