mlpack  master
sfinae_utility.hpp
Go to the documentation of this file.
1 
15 #ifndef MLPACK_CORE_SFINAE_UTILITY
16 #define MLPACK_CORE_SFINAE_UTILITY
17 
18 /*
19  * Constructs a template supporting the SFINAE pattern.
20  *
21  * This macro generates a template struct that is useful for enabling/disabling
22  * a method if the template class passed in contains a member function matching
23  * a given signature with a specified name.
24  *
25  * The generated struct should be used in conjunction with std::enable_if_t.
26  *
27  * For general references, see:
28  * http://stackoverflow.com/a/264088/391618
29  *
30  * For an mlpack specific use case, see /mlpack/core/util/prefixedoutstream.hpp
31  * and /mlpack/core/util/prefixedoutstream_impl.hpp
32  *
33  * @param NAME the name of the struct to construct. For example: HasToString
34  * @param FUNC the name of the function to check for. For example: ToString
35  */
36 #define HAS_MEM_FUNC(FUNC, NAME) \
37 template<typename T, typename sig> \
38 struct NAME { \
39  typedef char yes[1]; \
40  typedef char no [2]; \
41  template<typename U, U> struct type_check; \
42  template<typename _1> static yes &chk(type_check<sig, &_1::FUNC> *); \
43  template<typename > static no &chk(...); \
44  static bool const value = sizeof(chk<T>(0)) == sizeof(yes); \
45 };
46 
47 #endif