Skip to content

Instantly share code, notes, and snippets.

@taketwo
Created September 14, 2015 20:50
Show Gist options
  • Save taketwo/0d840b7bff3139af7e87 to your computer and use it in GitHub Desktop.
Save taketwo/0d840b7bff3139af7e87 to your computer and use it in GitHub Desktop.
A c++11 metafunction to check if a class has a method
// Create a metafunction "has_xxx_method" that can be used
// to check if a class has a method called "xxx". Note the
// limitation: only existence of parameterless methods can
// be checked this way.
// Credits: http://stackoverflow.com/a/21827727/1525865
#define HAS_METHOD_DEF(xxx) \
template<typename T> \
constexpr auto has_ ## xxx ## _method(int) \
-> decltype(std::declval<T>(). xxx (), bool()) \
{ return true; } \
template<typename T> \
constexpr bool has_ ## xxx ## _method(...) \
{ return false; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment