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