    #include <iostream>
    #include <utility>
 
    struct Foo {};
    struct Bar { Foo _foo; };
    struct Bad {};
    
    template <typename T, typename U> struct same_type: std::false_type {};
    
    template <typename T> struct same_type<T, T>: std::true_type {};
 
    Foo const& to_foo(Bar const& b) { return b._foo; }
    Foo& to_foo(Bar& b) { return b._foo; }
 
    template <typename T>
    struct ToFoo {
      T const& _crt;
      T& _rt;
 
      template <typename U>
      static auto to_foo_exists(U const& crt, U& rt) ->
          decltype(to_foo(crt), to_foo(rt), std::true_type());
 
      static std::false_type to_foo_exists(...);
 
      static bool const value = same_type<decltype(to_foo_exists(_crt, _rt)), std::true_type>::value;
    };
 
    int main() {
      std::cout << ToFoo<Bar>::value << "\n";
      std::cout << ToFoo<Bad>::value << "\n";
    }