[英]template non-type template parameters
以下是我注册免费 function 或成员 function 作为回调的代码。
在这里找到代码https://cppinsights.io/s/58dcf235
#include <stdio.h>
#include <iostream>
#include <functional>
#include <vector>
using namespace std;
class IEvent
{
public:
int m_EventType;
virtual ~IEvent() {}
};
template<class...Args>
class Event : public IEvent {};
template<int eventType,class...Args>
class Event<int eventType, bool(Args...)> : public IEvent
{
public:
Event(bool(*func)(Args...)) :m_FnPtr(func)
{
m_EventType = eventType;
m_ListenersList.push_back(m_FnPtr);
}
template <typename T>
Event(T* obj, bool(T::* Func)(Args...))
{
m_EventType = eventType;
m_FnPtr = [obj, Func](Args&&... args)->bool {
return (obj->*Func)(std::forward<Args>(args)...);
};
m_ListenersList.push_back(m_FnPtr);
}
void NotifyListeners(Args&&...args) const
{
for (auto& itr : m_ListenersList)
{
(itr)(std::forward<Args>(args)...);
}
}
private:
std::function<bool(Args...)> m_FnPtr;
std::vector<std::function<bool(Args...)>> m_ListenersList;
};
class Window
{
public:
bool OnKeyUp(bool, double)
{
cout << endl << "Member Function called";
return true;
}
bool OnClicked()
{
cout << endl << "OnClicked";
return true;
}
//using KeyupListenerType = Event<"KeyUp", bool(bool, double)>;
};
int main()
{
Window w;
Event<90,bool(bool, double)> evnt(&w, &Window::OnKeyUp);
//Event<100,bool()> evnt(&w, &Window::OnClicked);
evnt.NotifyListeners(true, 6.8);
return 0;
}
但我在线上遇到错误:
Event<90,bool(bool, double)> evnt(&w, &Window::OnKeyUp);
我正在尝试将事件类型分配给事件侦听器,如下所示。 我想在实例化过程中分配事件类型。 但我收到以下错误
26 | class Event<int, bool(Args...)> : public IEvent
| ^~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:26:7: note: ‘eventType’
main.cpp: In function ‘int main()’:
main.cpp:80:32: error: type/value mismatch at argument 1 in template parameter list for ‘template class Event’
80 | Event<90,bool(bool, double)> evnt(&w, &Window::OnKeyUp);
| ^
main.cpp:80:32: note: expected a type, got ‘90’
main.cpp:80:62: error: expression list treated as compound expression in initializer [-fpermissive]
80 | Event<90,bool(bool, double)> evnt(&w, &Window::OnKeyUp);
| ^
main.cpp:80:62: error: cannot convert ‘bool (Window::*)(bool, double)’ to ‘int’ in initialization
main.cpp:81:10: error: request for member ‘NotifyListeners’ in ‘evnt’, which is of non-class type ‘int’
81 | evnt.NotifyListeners(true, 6.8);
| ^~~~~~~~~~~~~~~
我在做什么错误? 如何传递非类型参数?
您的基本声明与您的专业不匹配。
基本实现具有template <class...Args>
而template <int eventType, class...Args>
。
您还在此处的专业化声明中添加了一个不属于此处的额外int
:
template<int eventType,class...Args>
class Event<int eventType, bool(Args...)> : public IEvent
^^^ here
调整后的代码如下所示
#include <stdio.h>
#include <iostream>
#include <functional>
#include <vector>
using namespace std;
class IEvent
{
public:
int m_EventType;
virtual ~IEvent() {}
};
template<int eventType, class...Args>
class Event : public IEvent {};
template<int eventType,class...Args>
class Event<eventType, bool(Args...)> : public IEvent
{
public:
Event(bool(*func)(Args...)) :m_FnPtr(func)
{
m_EventType = eventType;
m_ListenersList.push_back(m_FnPtr);
}
template <typename T>
Event(T* obj, bool(T::* Func)(Args...))
{
m_EventType = eventType;
m_FnPtr = [obj, Func](Args&&... args)->bool {
return (obj->*Func)(std::forward<Args>(args)...);
};
m_ListenersList.push_back(m_FnPtr);
}
void NotifyListeners(Args&&...args) const
{
for (auto& itr : m_ListenersList)
{
(itr)(std::forward<Args>(args)...);
}
}
private:
std::function<bool(Args...)> m_FnPtr;
std::vector<std::function<bool(Args...)>> m_ListenersList;
};
class Window
{
public:
bool OnKeyUp(bool, double)
{
cout << endl << "Member Function called";
return true;
}
bool OnClicked()
{
cout << endl << "OnClicked";
return true;
}
//using KeyupListenerType = Event<"KeyUp", bool(bool, double)>;
};
int main()
{
Window w;
Event<90,bool(bool, double)> evnt(&w, &Window::OnKeyUp);
//Event<100,bool()> evnt(&w, &Window::OnClicked);
evnt.NotifyListeners(true, 6.8);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.