fork download
  1. #include<type_traits>
  2.  
  3. class foo {
  4. };
  5.  
  6. class bar {
  7. public:
  8. /*
  9.   const foo & to_foo() const {
  10.   return f;
  11.   }
  12. */
  13. foo & to_foo() {
  14. return f;
  15. }
  16. private:
  17. foo f;
  18. };
  19.  
  20. template< typename T, typename Enable = void >
  21. class convert {};
  22.  
  23. template< typename T >
  24. struct convert< T, typename std::enable_if< std::is_member_function_pointer< decltype( &T::to_foo ) >::value >::type > {
  25. static const foo & call1( const bar & b ) {
  26. return b.to_foo();
  27. }
  28.  
  29. static foo & call2( bar & b ) {
  30. return b.to_foo();
  31. }
  32. };
  33.  
  34. int main() {
  35. bar b;
  36. foo & f = convert<bar>::call2( b );
  37. return 0;
  38. }
Success #stdin #stdout 0s 2824KB
stdin
Standard input is empty
stdout
Standard output is empty