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. static std::string get_x() {
  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. static std::string get_x() {
  23. std::cout << "Invoking 2" << std::endl;
  24. return T::x;
  25. }
  26. };
  27.  
  28. template <typename T>
  29. struct bar<T*, void> {
  30. static std::string get_x() {
  31. return bar<T>::get_x();
  32. }
  33. };
  34.  
  35. int main() {
  36. // your code goes here
  37. //bar<foo> y;
  38. bar<foo*> z;
  39. std::cout << z.get_x() << std::endl;
  40. return 0;
  41. }
  42.  
  43. struct foo {
  44. static constexpr const char* x = "hello";
  45. };
  46.  
  47.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Invoking 2
hello