[英]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.