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