Skip to content

Instantly share code, notes, and snippets.

@2bbb
Created December 18, 2015 02:34
Show Gist options
  • Select an option

  • Save 2bbb/c9a5c922495fc717615b to your computer and use it in GitHub Desktop.

Select an option

Save 2bbb/c9a5c922495fc717615b to your computer and use it in GitHub Desktop.
method_checker_macro
#define method_checker(name, method, result, ...)\
template <typename T>\
struct name {\
template <typename testee, result (testee::*)(__VA_ARGS__) = &testee::method> struct tester {};\
template <typename testee> static std::true_type test(tester<testee> *);\
template <typename> static std::false_type test(...);\
static constexpr bool has_method = decltype(test<T>(nullptr))::value;\
};
method_checker(has_update, update, void);
method_checker(has_update_i, update, void, int);
struct foo {};
struct bar { void update(); };
struct baz { void update(int); };
int main(int argc, char *argv[]) {
std::cout << has_update<foo>::has_method << std::endl;
std::cout << has_update<bar>::has_method << std::endl;
std::cout << has_update<baz>::has_method << std::endl;
std::cout << has_update_i<bar>::has_method << std::endl;
std::cout << has_update_i<baz>::has_method << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment