Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename Ret>
- struct AnyCallable
- {
- template<typename ... Args>
- AnyCallable(std::function<Ret(Args...)> fun) : m_any(fun) {}
- template<typename ... Args>
- Ret operator()(Args && ... args) { return std::any_cast<std::function<Ret(Args...)>>(m_any)(std::forward<Args...>(args...)); }
- std::any m_any;
- }
- template<>
- struct AnyCallable<void>
- {
- template<typename ... Args>
- AnyCallable(std::function<void(Args...)> fun) : m_any(fun) {}
- template<typename ... Args>
- void operator()(Args && ... args) { std::any_cast<std::function<void(Args...)>>(m_any)(std::forward<Args...>(args...)); }
- std::any m_any;
- }
- #include "ffg.h"
- void foo(int x, int y)
- {
- //do something
- }
- void bar(std::string x, int y, int z)
- {
- //do something
- }
- int main()
- {
- std::map<std::string, AnyCallable<void>> map;
- map["foo"] = &foo; //store the methods in the map
- map["bar"] = &bar;
- map["foo"](1, 2); //call them with parameters I get at runtime
- map["bar"]("Hello", 1, 2);
- }
Add Comment
Please, Sign In to add comment