fork(1) download
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class Server {
  7. private:
  8. using erasedType = void (*)();
  9. unordered_map<string, erasedType> funcs;
  10. public:
  11. template<typename... Args>
  12. void Register(const string& name, void (*func)(Args...)) {
  13. funcs[name] = reinterpret_cast<erasedType>(func);
  14. }
  15.  
  16. template<typename... Args>
  17. void Call(const string& name, Args... args) {
  18. using funcType = void (*)(Args...);
  19. auto func = reinterpret_cast<funcType>(funcs.at(name));
  20. return func(args...);
  21. }
  22. };
  23.  
  24. int main() {
  25. Server server;
  26. server.Register("Add", +[](int a, int b){ cout << a+b << endl; });
  27. server.Register("Echo", +[](string str){ cout << str << endl; });
  28.  
  29. server.Call("Add", 12, 13);
  30. server.Call("Echo", string("hello"));
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5380KB
stdin
Standard input is empty
stdout
25
hello