gpt4 book ai didi

c++ - 绑定(bind)可以用来转发可变数量的参数吗?

转载 作者:行者123 更新时间:2023-11-27 23:04:07 25 4
gpt4 key购买 nike

在 C++14 中,我可以编写一个对任意数量的参数进行完美转发的 lambda:

template<typename... Args>
void process(Args&&... args); // template to forward to


auto wrapper = [](auto&&... args)
{
std::cout << "Invoking lambda wrapper\n";
process(std::forward<decltype(args)>(args)...); // do the forwarding
};

有没有办法使用bind达到同样的效果?我知道 bind 创建的函数对象接受任意数量的参数,我知道这些对象使用完美转发未绑定(bind)的参数,但是有没有办法告诉 bind创建一个对传递给它的每个参数使用完美转发的函数对象,即使在对 bind?

的调用中没有占位符也是如此

最佳答案

目前不可能,但请参阅 http://cplusplus.github.io/EWG/ewg-complete.html#44支持它的建议:

As more variadic functions work their way into my C++ code, I'm getting increasingly annoyed that there isn't a variadic bind. There is a tiny bit of annoyance on exactly what to use. There seems to me to be 2 sensible choices (other people may have others)

1) _args : Use all otherwise unnamed arguments.
2) _3onwards : All arguments from the 3rd onwards.

I haven't personally found a need for multiple ranges of variadic arguments, or more complicated chopping (such as getting the last few arguments), and I'd want to hopefully keep this simple if possible!

关于c++ - 绑定(bind)可以用来转发可变数量的参数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24745331/

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