fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. template <typename T>
  6. struct check_valid_type {
  7. typedef void type;
  8. };
  9.  
  10. struct foo;
  11.  
  12. template <typename T, typename I=void>
  13. struct bar {
  14. std::string get_int() {
  15. std::cout << "Invoking 1" << std::endl;
  16. return "--";
  17. }
  18. };
  19.  
  20. template <typename T>
  21. struct bar<T, typename check_valid_type<decltype(T::x)>::type> {
  22. std::string get_int() {
  23. std::cout << "Invoking 2" << std::endl;
  24. return T::x;
  25. }
  26. };
  27.  
  28.  
  29.  
  30. int main() {
  31. // your code goes here
  32. bar<foo> y;
  33. std::cout << y.get_int() << std::endl;
  34. return 0;
  35. }
  36.  
  37. struct foo {
  38. static constexpr const char* x = "hello";
  39. };
  40.  
  41.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Invoking 1
--