I learned SFINAE can be used to determine whether a member function exists in a class or not. For example, the following code can be used to check if the method hello is present in a class.
struct has_method_hello {
  using yes = char[1];
  using no  = char[2];
  template <typename U, typename C>
  static constexpr yes& test(decltype(&U::hello));
  template <typename>
  static constexpr no& test(...);
  static constexpr bool value = (sizeof(yes) == sizeof(test<T>(nullptr)));
}; 
struct Foo {
  void hello() {}
}
std::cout << has_method_hello <Foo> :: value << std::endl;  // 1
However, suppose the hello is templated, how can I modify the trick so it can still function properly?
struct Foo {
  template <typename T>
  void hello(T&) {...}
}
Aucun commentaire:
Enregistrer un commentaire