Created
December 18, 2015 02:34
-
-
Save 2bbb/c9a5c922495fc717615b to your computer and use it in GitHub Desktop.
method_checker_macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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