online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS Code, Compile, Run and Debug online from anywhere in world. *******************************************************************************/ #include <iostream> #include <unordered_map> #include <typeindex> struct OutputType {}; struct EventType1 {}; struct EventType2 {}; struct EventType3 {}; OutputType event1(EventType1 event) { std::cout << "Called: " << __PRETTY_FUNCTION__ << "\n"; } OutputType event2(EventType2 event) { std::cout << "Called: " << __PRETTY_FUNCTION__ << "\n"; } // we store the function pointers as OutputType(*)() // but the type_index lookup returns a function pointer // belonging to EventType so it is safe to cast it back // to the real pointer type std::unordered_map<std::type_index,OutputType(*)()> handlers; template<typename EventType> void registerHandler(OutputType (*handler)(EventType)) { handlers.insert({std::type_index(typeid(EventType)),(OutputType(*)()) handler}); } template<typename EventType> OutputType callHandler(EventType event) { // we can use find and then check if it was found // or we can use at and let it throw std::out_of_range if it is not found auto it = handlers.find(std::type_index(typeid(EventType))); if (it == handlers.end()) return {}; // need to do something with this return ((OutputType(*)(EventType)) it->second)(event); } int main() { registerHandler(event1); registerHandler(event2); EventType1 event1; // will be found callHandler(event1); EventType2 event2; // will be found callHandler(event2); EventType3 event3; // won't be found callHandler(event3); return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text



Program is not being debugged. Click "Debug" button to start program in debug mode.