1

I have been spending quite a few hours now searching for a solution to this problem: My compiler keeps complaining about a missing template argument list, obviously it is not able to deduce the template types from the arguments themselves.

Here is the code:

template <typename T> class SafeCallback {
public:
  SafeCallback(std::shared_ptr<T> shared, void (T::*mf)())
      : weak_(shared), mf_(mf) {}

  void Call() {
    if (auto shared = weak_.lock()) {
      (shared.get()->*mf_)();
    }
  }

private:
  std::weak_ptr<T> weak_;
  void (T::*mf_)();
};

class A {
public:
  void do_something() {
    int i = 0;
    i++;
    return;
  }
};

int main() {
  std::shared_ptr<A> a_ptr = std::make_shared<A>();
  SafeCallback<A>(a_ptr, &A::do_something).Call(); // ok
  SafeCallback(a_ptr, &A::do_something).Call(); // error
  return 0;
}

Any suggestions?

0 Answers0