在 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/