fork download
  1. #include <iostream>
  2. #include <utility>
  3.  
  4. struct Foo {};
  5. struct Bar { Foo _foo; };
  6. struct Bad {};
  7.  
  8. template <typename T, typename U> struct same_type: std::false_type {};
  9.  
  10. template <typename T> struct same_type<T, T>: std::true_type {};
  11.  
  12. Foo const& to_foo(Bar const& b) { return b._foo; }
  13. Foo& to_foo(Bar& b) { return b._foo; }
  14.  
  15. template <typename T>
  16. struct ToFoo {
  17. T const& _crt;
  18. T& _rt;
  19.  
  20. template <typename U>
  21. static auto to_foo_exists(U const& crt, U& rt) ->
  22. decltype(to_foo(crt), to_foo(rt), std::true_type());
  23.  
  24. static std::false_type to_foo_exists(...);
  25.  
  26. static bool const value = same_type<decltype(to_foo_exists(_crt, _rt)), std::true_type>::value;
  27. };
  28.  
  29. int main() {
  30. std::cout << ToFoo<Bar>::value << "\n";
  31. std::cout << ToFoo<Bad>::value << "\n";
  32. }
Success #stdin #stdout 0s 2828KB
stdin
Standard input is empty
stdout
1
0