#include <iostream>
#include <algorithm>
#include <memory>
#include <mutex>
#include <vector>
template <typename Observer>
class ObsNoti {
public:
template <
typename Ret,
typename Class,
typename... Args,
typename = std::enable_if_t<std::is_same<Observer, Class>::value>>
void notifyEveryone(Ret Class::*func, Args&&... args) {
(obs.*func)(std::forward<Args>(args)...);
}
Observer obs;
};
class ContactObserver {
public:
virtual int onContactRetrieved() {
std::cout<<"onContactRetrieved\n";
return 0;
}
virtual int onContactRetrieved(bool bSucess) {
std::cout<<"onContactRetrieved sucessfully\n";
return 0;
}
};
class MyClass : public ObsNoti<ContactObserver> {
public:
template<typename Func, bool, typename... Args>
inline void notifyObserver(Func &&func, bool b, Args&&... args) {
ObsNoti<ContactObserver>::notifyEveryone(func , b, std::forward<Args>(args)...);
}
template<typename Func, typename... Args>
inline void notifyObserver(Func &&func, Args&&... args) {
ObsNoti<ContactObserver>::notifyEveryone(func , std::forward<Args>(args)...);
}
};
int main() {
MyClass my;
my.notifyObserver(&ContactObserver::onContactRetrieved, true);
return 0;
}
我收到以下错误:
/tmp/w7Rta4F1cT.cpp:53:22: error: no matching function for call to 'MyClass::notifyObserver(<unresolved overloaded function type>, bool)'
53 | my.notifyObserver(&ContactObserver::onContactRetrieved, true);
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/w7Rta4F1cT.cpp:37:17: note: candidate: 'template<class Func, bool <anonymous>, class ... Args> void MyClass::notifyObserver(Func&&, bool, Args&& ...)'
37 | inline void notifyObserver(Func &&func, bool b, Args&&... args) {
| ^~~~~~~~~~~~~~
/tmp/w7Rta4F1cT.cpp:37:17: note: template argument deduction/substitution failed:
/tmp/w7Rta4F1cT.cpp:53:22: note: couldn't deduce template parameter 'Func'
53 | my.notifyObserver(&ContactObserver::onContactRetrieved, true);
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/w7Rta4F1cT.cpp:42:17: note: candidate: 'template<class Func, class ... Args> void MyClass::notifyObserver(Func&&, Args&& ...)'
42 | inline void notifyObserver(Func &&func, Args&&... args) {
| ^~~~~~~~~~~~~~
/tmp/w7Rta4F1cT.cpp:42:17: note: template argument deduction/substitution failed:
/tmp/w7Rta4F1cT.cpp:53:22: note: couldn't deduce template parameter 'Func'
53 | my.notifyObserver(&ContactObserver::onContactRetrieved, true);
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我知道这里的问题是因为重载,模板在编译时无法解析函数。但是,我不确定正确的实现方法。
有办法解决吗?
1条答案
按热度按时间68bkxrlz1#
你的代码有很多问题:
ObsNoti::notifyEveryone
被定义为接受指向成员变量的指针,而不是指向成员函数的指针。ContactObserver::onContactRetrieved
的重载使值&ContactObserver::onContactRetrieved
不明确,在模板参数推导期间无法解决此问题。要修复1:
要修复2,您需要使用static_cast来选择适当的重载:
https://godbolt.org/z/azYGM3cjz
或通过显式提供类型来防止第一个参数的推导:
https://godbolt.org/z/E3avEbhr5