fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. template <typename T>
  5. struct id { typedef T type; };
  6.  
  7. template <typename T>
  8. struct has_type {
  9. private:
  10. template <typename T1>
  11. static id<typename T1::type> f(int);
  12. template <typename>
  13. static void f(...);
  14. public:
  15. enum { value = !std::is_void<decltype(f<T>(0))>::value };
  16. };
  17.  
  18. template <typename T, bool B = has_type<T>::value>
  19. struct wavefunction_collapse : wavefunction_collapse<typename T::type> {};
  20.  
  21. template <typename T>
  22. struct wavefunction_collapse<T, false> { typedef T type; };
  23.  
  24. template <int N>
  25. struct foo { typedef foo<N+1> type; enum { value = N }; };
  26.  
  27. template <>
  28. struct foo<10> { enum { value = 42 }; };
  29.  
  30. int main() {
  31. std::cout << wavefunction_collapse<foo<0>>::type::value;
  32. }
  33.  
Success #stdin #stdout 0s 2828KB
stdin
Standard input is empty
stdout
42