fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <vector>
  5.  
  6. template <typename E>
  7. std::false_type has_a_impl(...);
  8.  
  9. template <typename E> auto has_a_impl(int) -> decltype(E::a, std::true_type{});
  10.  
  11. template <typename E>
  12. using has_a = decltype(has_a_impl<E>(0));
  13.  
  14.  
  15. template <typename E>
  16. std::enable_if_t<has_a<E>::value, int>
  17. get_a_int_val() { return static_cast<int>(E::a); }
  18.  
  19. template <typename E>
  20. std::enable_if_t<!has_a<E>::value, int>
  21. get_a_int_val() { return 42; }
  22.  
  23. enum class E
  24. {
  25. a = 0
  26. };
  27.  
  28. int main()
  29. {
  30. std::cout << get_a_int_val<E>() << std::endl;
  31. std::cout << get_a_int_val<int>() << std::endl;
  32. }
  33.  
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
0
42