fork download
  1. #include<memory>
  2. #include<iostream>
  3. #include<typeinfo>
  4. using namespace std;
  5.  
  6. template<typename T>
  7. struct void_ { typedef void type; };
  8.  
  9. template<typename T, typename = void>
  10. struct My_sfinae {
  11. typedef T RealType;
  12. static T& getPtr (T &p) { return p; }
  13. }; //^^^^^^^^^ business logic!
  14. template<typename T>
  15. struct My_sfinae<T, typename void_<typename T::smart_type>::type> {
  16. typedef typename My_sfinae<typename T::smart_type>::RealType RealType;
  17. static RealType& getPtr (T &p) { return My_sfinae<typename T::smart_type>::getPtr(p.getPtr()); }
  18. };
  19.  
  20. template<typename T>
  21. class some_smart_ptr_wrapper
  22. {
  23. T m_ptr;
  24. public:
  25. typedef T smart_type; // <-- unique name
  26. T& getPtr () { return m_ptr; }
  27.  
  28. typename My_sfinae<T>::RealType& getFinalPtr () { return My_sfinae<T>::getPtr(m_ptr); }
  29. };
  30.  
  31. int main ()
  32. {
  33. shared_ptr<int> p1;
  34. some_smart_ptr_wrapper<some_smart_ptr_wrapper<some_smart_ptr_wrapper<shared_ptr<int>>>> p2;
  35.  
  36. if(typeid(p1) == typeid(p2.getFinalPtr()))
  37. cout << "working\n";
  38. else
  39. cout << "not working\n";
  40. }
  41.  
Success #stdin #stdout 0s 2928KB
stdin
Standard input is empty
stdout
working