Request Short Link
C++ 98
C++ 11
C++ 14
C++ 17
C++ 20
C++ 23
C++ 2c
for-loops as while-loops
array subscription
Show all implicit casts
Show all template parameters of a CallExpr
Use libc++
Transform std::initializer_list
Show noexcept internals
Show padding information
Show coroutine transformation
Show C++ to C transformation
Show object lifetime
Default
15
18
20
22
26
More
GitHub
Patreon
Issues
About
Policies
Examples
C++ Insights @ YouTube
Settings
Version
Workshop: Safe and Efficient C++ for Embedded Environments
×
Made by
Andreas Fertig
Powered by
Flask
and
CodeMirror
Source:
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl, C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog. Code, Compile, Run and Debug online from anywhere in world. *******************************************************************************/ #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, 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 GuiWindow { 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() { GuiWindow w; Event<90,bool(bool, double)> evnt(&w, &GuiWindow::OnKeyUp); evnt.NotifyListeners(true, 6.8); return 0; }
Insight:
Console: