无法通过“变量”将参数传递给基于variadic的模板

时间:2017-05-02 16:26:58

标签: c++ c++11 templates variadic-templates variadic-functions

我编写了一些代码,用于存储一些带有参数的任务,以便以后执行。代码:

class TaskInterface
{
public:
    virtual void Execute() = 0;
};

namespace TaskHelper
{
    template <std::size_t... Types>
    struct index {};

    template <std::size_t N, std::size_t... Types>
    struct gen_seq : gen_seq<N - 1, N - 1, Types...> {};

    template <std::size_t... Types>
    struct gen_seq<0, Types...> : index<Types...>{};
}

template <typename ReturnType, typename... Types>
class SimpleTask : public TaskInterface
{
public:
    template <typename Function>
    SimpleTask(Function&& func, Types&&... args)
        : m_function(std::forward<Function>(func)),
        m_args(std::make_tuple(std::forward<Types>(args)...)) {
    }

    void Execute() override final
    {
        func(m_args);
    }

private:
    std::function<ReturnType(Types...)> m_function;
    std::tuple<Types...> m_args;

    template <typename... Args, std::size_t... Is>
    void func(std::tuple<Args...>& tup, TaskHelper::index<Is...>)
    {
        m_function(std::get<Is>(tup)...);
    }

    template <typename... Args>
    void func(std::tuple<Args...>& tup)
    {
        func(tup, TaskHelper::gen_seq<sizeof...(Args)>{});
    }
};

template < typename ReturnType, class Class, typename... Types>
class MemberTask : public TaskInterface
{
public:
    typedef ReturnType(Class::*Method)(Types...);

    MemberTask(Class* object, Method method, Types&&... args) :
        m_object(object), m_method(method), m_args(std::make_tuple(std::forward<Types>(args)...)) {
    };

    void Execute() override final
    {
        func(m_args);
    };

private:
    Class* m_object;
    Method m_method;
    std::tuple<Types...> m_args;

    template <typename... Args, std::size_t... Is>
    void func(std::tuple<Args...>& tup, TaskHelper::index<Is...>)
    {
        (m_object->*m_method)(std::get<Is>(tup)...);
    }

    template <typename... Args>
    void func(std::tuple<Args...>& tup)
    {
        func(tup, TaskHelper::gen_seq<sizeof...(Args)>{});
    }
};

template <typename Function, typename... Arguments>
TaskInterface* CreateSimpleTask(Function&& func, Arguments&&... args)
{
    return new SimpleTask<typename std::result_of<decltype(func)(Arguments...)>::type, Arguments...>(std::forward<Function>(func), std::forward<Arguments>(args)...);
}

template <class Class, typename Method, typename... Arguments>
TaskInterface* CreateMemberTask(Class* obj, Method method, Arguments&&... args)
{
    return new MemberTask<typename std::result_of<decltype(method)(Class)>::type, Class, Arguments...>(std::forward<Class*>(obj), std::forward<Method>(method), std::forward<Arguments>(args)...);
}

class Test {
public:
    Test() { id = ++m_id; }
    bool doIt(int n) {
        std::cout << "doIt of " << n * id;
        return true;
    };

private:
    static int m_id;
    int id;
};

int Test::m_id = 0;


double test1(int xs)
{
    xs *= 555;
    return 66.02l;
}

但问题是我只能以这种方式创建这些任务:

TaskInterface* st = CreateSimpleTask(test1, 5);

Test t;
TaskInterface* mt = CreateMemberTask(&t, &Test::doIt, 66);

并不能以这种方式:

// error C2664: 'double (int)' : cannot convert argument 1 from 'int *' to 'int'
int xxxx;
TaskBase* st = CreateSimpleTask(test1, &xxxx);

或对于MemberTask:

// cannot convert argument 2 from 'bool (__thiscall Test::* )(std::string)' to 'bool (__thiscall Test::* )(std::string &)'
std::string ss = "sdfsdf";
TaskBase* mt = CreateMemberTask(&t, &Test::doIt, ss);

如何修改我的类,以便不仅通过“值”传递参数,还可以通过“变量”传递参数?或者我的所有“架构”都是完全错误的?

1 个答案:

答案 0 :(得分:1)

比存储具有特定签名及其参数的函数更简单的方法是简单地存储一个没有参数及其上下文的函数(有时称为thunk)。

std::function<void()> st = [] { test1(5); };
std::function<void()> mt = [&] { t.doIt(ss); };

与您的代码不同,这更简单,并且不会泄漏内存。您可以按值,按引用或组合捕获上下文。 Lambda很酷! http://en.cppreference.com/w/cpp/language/lambda