Pass tuple's content as variadic function arguments

后端 未结 1 1952
醉话见心
醉话见心 2020-12-20 09:25

I play with C++0x for some time and now I want to use variadic templates and tuple to implement class \"Task\". I\'m going to pass Task objects into newly created threads (u

相关标签:
1条回答
  • 2020-12-20 09:55

    It should be equivalent, but the only way to be sure is to test with the compiler you are using.

    Note that you could use std::function with std::bind instead, e.g. something like:

    template<typename ...T_arguments> class Task : public TaskCaller
    {
        std::function<bool (T_arguments&...)> functor;
    public:
        Task (bool (*func)(T_arguments&...), T_arguments... arguments)
          : functor(std::bind(func, arguments...))
        {}
        bool dispatch() {
            return functor();
        }
        // ...
    

    Or better yet, let the user pass a std::function in:

    class Task : public TaskCaller {
        std::function<bool ()> functor;
    public:
        Task(std::function<bool ()> func) : functor(func) {}
        // ...
    

    That allows the user to choose what to pass instead of forcing him to use free functions or static member functions.

    0 讨论(0)
提交回复
热议问题