-1

Yea, how is this done?

The stuff I looked up had pretty concretely defined functions. For one, I don't know how to call the function with the va_arg list. For two, I don't know what it's supposed to look like.

I want to be able to do

event.register(func, arg1, arg2, arg3, ...);

for any function without the use of templates, just like std::thread does it.

How do I do this?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
nestharus
  • 217
  • 2
  • 11
  • 3
    `std::thread` does it with variadic templates args – quantdev Oct 05 '14 at 02:10
  • 1
    Take a [`std::function`](http://en.cppreference.com/w/cpp/utility/functional/function) parameter. – πάντα ῥεῖ Oct 05 '14 at 02:11
  • _"without the use of templates, just like std::thread does it."_ As mentioned, `std::thread` ***actually uses templates*** of course. Also have a look at [perfect forwarding](http://stackoverflow.com/questions/7038357/make-unique-and-perfect-forwarding). – πάντα ῥεῖ Oct 05 '14 at 02:16

1 Answers1

0

Solved

class Event
{
    list<function<void()>> functions;
public:
    Event()
    {
    } //Event

    template<typename T, typename... Args>
    void reg(T& func, Args... args)
    {
        functions.push_back(bind(func, args...));
    } //reg

    void execute()
    {
        for (auto& func : functions)
        {
            func();
        } //for
    } //execute
};

=)

Event event;

event.reg(test, 2, 2.5);
event.reg(*[](int n, const char* foo){ cout << n << " " << foo << endl; }, 5, "rawr");
event.execute();
nestharus
  • 217
  • 2
  • 11