mlpack  2.2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 #include <boost/utility/enable_if.hpp>
19 #include <boost/type_traits.hpp>
20 
21 /*
22  * Constructs a template supporting the SFINAE pattern.
23  *
24  * This macro generates a template struct that is useful for enabling/disabling
25  * a method if the template class passed in contains a member function matching
26  * a given signature with a specified name.
27  *
28  * The generated struct should be used in conjunction with boost::disable_if and
29  * boost::enable_if. Here is an example usage:
30  *
31  * For general references, see:
32  * http://stackoverflow.com/a/264088/391618
33  *
34  * For an mlpack specific use case, see /mlpack/core/util/prefixedoutstream.hpp
35  * and /mlpack/core/util/prefixedoutstream_impl.hpp
36  *
37  * @param NAME the name of the struct to construct. For example: HasToString
38  * @param FUNC the name of the function to check for. For example: ToString
39  */
40 #define HAS_MEM_FUNC(FUNC, NAME) \
41 template<typename T, typename sig> \
42 struct NAME { \
43  typedef char yes[1]; \
44  typedef char no [2]; \
45  template<typename U, U> struct type_check; \
46  template<typename _1> static yes &chk(type_check<sig, &_1::FUNC> *); \
47  template<typename > static no &chk(...); \
48  static bool const value = sizeof(chk<T>(0)) == sizeof(yes); \
49 };
50 
51 #endif