fork download
  1. #include <utility>
  2. #include <memory>
  3. #include <iostream>
  4.  
  5. template<class T>
  6. T&& smart_wrap_unwrap(T&& v, long)
  7. {
  8. return std::forward<T>(v);
  9. }
  10.  
  11. template<class T>
  12. auto smart_wrap_unwrap(T&& v, int)
  13. -> decltype(smart_wrap_unwrap(std::forward<T>(v).getPtr(), 0))
  14. {
  15. return smart_wrap_unwrap(std::forward<T>(v).getPtr(), 0);
  16. }
  17.  
  18. template<class T>
  19. struct smart{
  20. template<class U>
  21. smart(U* v) : _ptr(v){}
  22. T& getPtr(){ return _ptr; }
  23. T _ptr;
  24. };
  25.  
  26. struct X{
  27. void foo(){ std::cout << "!\n"; }
  28. };
  29.  
  30. int main(){
  31. smart<smart<smart<std::shared_ptr<X>>>> smartception(new X());
  32. smart_wrap_unwrap(smartception, 0)->foo();
  33. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'decltype (smart_wrap_unwrap(forward<T>(v).getPtr(), 0)) smart_wrap_unwrap(T&&, int) [with T = smart<smart<smart<std::shared_ptr<X> > > >&, decltype (smart_wrap_unwrap(forward<T>(v).getPtr(), 0)) = smart<smart<std::shared_ptr<X> > >&]':
prog.cpp:32:36:   instantiated from here
prog.cpp:15:58: error: invalid initialization of reference of type 'std::shared_ptr<X>&' from expression of type 'smart<smart<std::shared_ptr<X> > >'
stdout

Standard output is empty