如何将可变参数模板参数绑定到函数 [英] How to bind variadic template params to function

查看:64
本文介绍了如何将可变参数模板参数绑定到函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模仿std :: thread构造函数的功能:

I'm trying to mimic std::thread constructor functionality:

template< class Function, class... Args > 
explicit thread( Function&& f, Args&&... args );

我曾尝试与调试器一起使用,以了解其工作原理,但我无法弄清.

I've tried stepping with debugger to see how it works but I couldn't figure it out.

如何像线程的构造函数那样创建和存储绑定类型?

How can I create and store bind type like thread's constructor does ?

类似这样的东西(语法可能不正确):

Something like this (the syntax maybe wrong):

class myClass{
private:
auto bindType;

public:
template< class Function, class... Args > 
explicit myClass( Function&& f, Args&&... args ) : bindType(somehowBind(f, args) {}
void evaluate() {bindType();}
};

用法示例:

int test(int i) {return i;}

int main(){
myClass my(test, 5);
my.evaluate();
}

请注意,我不在乎 somehowBind 函数是否会忽略返回类型,即其返回类型可以类似于std :: function.我所要做的就是了解如何将 class ... Args 绑定到给定的函数 f ,以便在调用 somehowBind 之后会像std :: bind一样.为了阐明我的观点,您可以考虑以下几点:

Note that I don't care if somehowBind function will ignore the return type i.e. its return type can be something like std::function. All I wan't to do is understand how I can bind class... Args to a given function f such that after calling somehowBind it will act like std::bind does. To clarify my point you can think about what I'm trying to achieve as follow:

thread t(test, 5); // unlike the usual std:::thread, this one is created in suspended mode therefore I need somehow to bind `f` with `5` and store it
t.start(); // now t is executed

这有点想起C#和Java线程,它们在构造后不会立即执行.

It's kinda reminds C# and Java threads, they not executed right after construction.

推荐答案

对于初学者来说,您可以使用 std :: bind 将某些参数绑定到函数上,而您必须进行simpy操作:

For starters, to bind some parameters to a function using std::bind you simpy do:

// Some function.
void printValues(int x, double y) {
    std::cout << x << " " << y << std::endl;
}

auto func = std::bind(printValues, 5, 2.0); // Bind params and return functor.
func(); // Evaluate function call (returns void in this case).

接下来,要将函子及其参数存储在类中,并且您在评估时无需关心返回值,只需使用lambda表达式来包装 std :: bind 表达式(lambda用于删除返回值):

Next, to store a functor and its parameters in a class and you don't care about the return value when evaluating then simply use a lambda expression to wrap the std::bind expression (the lambda is used to drop the return value):

struct Foo {
    template <typename Function, typename... Args>
    Foo(Function&& func, Args&&... args) {
        auto f = std::bind(std::forward<Function>(func), std::forward<Args>(args)...);
        func_ = [f] { f(); };
        // func_ = [f{std::move(f)}] { f(); }; // In C++14 you can move capture.
    }
    void evaluate() { func_(); }
    std::function<void()> func_;
};

> 也请参见此实时示例

如果您要存储可变包装,,请参见以下答案:如何存储可变参数模板参数?

If you're looking to store a variadic pack then see this answer: How to store variadic template arguments?

这篇关于如何将可变参数模板参数绑定到函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆