模板非类型模板参数

template non-type template parameters

以下是我将自由函数或成员函数注册为回调的代码。

在此处查找代码 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;
}