fork download
  1. #include <memory>
  2. #include <iostream>
  3.  
  4. template<class T>
  5. struct call_with_pointer {
  6. // last resort: T*
  7. template<class Callable>
  8. static auto call(Callable &callable, const std::shared_ptr<T> &param) -> decltype(callable(param.get())) {
  9. return callable(param.get());
  10. }
  11. };
  12.  
  13. template<class T>
  14. struct call_with_shared : public call_with_pointer<T> {
  15.  
  16. // best: call with shared_ptr<T>.
  17. // SFINA
  18. template<class Callable>
  19. static auto call(Callable &callable, const std::shared_ptr<T> &param) -> decltype(callable(param)) {
  20. return callable(param);
  21. }
  22.  
  23. using call_with_pointer<T>::call;
  24.  
  25. };
  26.  
  27.  
  28. class Test {
  29. public:
  30. bool operator () (int * x) {
  31. return *x == 42;
  32. }
  33. };
  34.  
  35. using namespace std;
  36.  
  37. int main (int argc, const char * argv[])
  38. {
  39. {
  40. Test t;
  41.  
  42. auto i = std::make_shared<int>(4);
  43.  
  44. auto x = call_with_shared<int>::call(t, i);
  45. }
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 3060KB
stdin
Standard input is empty
stdout
Standard output is empty