fork(7) download
  1. #include <type_traits>
  2. #include <utility>
  3. #include <iostream>
  4.  
  5. template <typename T, typename = void>
  6. struct foo_impl {};
  7.  
  8. template <typename T>
  9. inline auto foo(T x) -> decltype(foo_impl<T>{}(x))
  10. {
  11. return foo_impl<T>{}(x);
  12. }
  13.  
  14. template <typename T>
  15. struct foo_impl<T,typename std::enable_if<std::is_integral<T>::value>::type>
  16. {
  17. void operator()(T) {}
  18. };
  19.  
  20. template <typename T>
  21. struct has_foo
  22. {
  23. struct yes {};
  24. struct no {};
  25. template <typename T1>
  26. static auto test(T1 x) -> decltype(foo(x),void(),yes{});
  27. static no test(...);
  28. static const bool value = std::is_same<yes,decltype(test(std::declval<T>()))>::value;
  29. };
  30.  
  31. template <typename T>
  32. struct foo_impl<T,typename std::enable_if<has_foo<unsigned>::value && std::is_floating_point<T>::value>::type>
  33. {
  34. void operator()(T) {}
  35. };
  36.  
  37. int main()
  38. {
  39. std::cout << has_foo<float>::value << '\n';
  40. std::cout << has_foo<int>::value << '\n';
  41. foo(3.1);
  42. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
1
1