gpt4 book ai didi

c++ - 无法通过 "variables"将参数传递给基于可变参数的模板

转载 作者:行者123 更新时间:2023-11-30 05:13:50 24 4
gpt4 key购买 nike

我已经编写了用于存储一些任务及其参数的代码,以便稍后执行。代码:

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);

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

最佳答案

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

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

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

关于c++ - 无法通过 "variables"将参数传递给基于可变参数的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43742914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com