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

标签 c++ c++14 perfect-forwarding stdbind

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

相关文章:

c++ - 有没有办法结合 Qt-Creator + Boost 库?

c++ - 通用 lambda 和一元 + 运算符

c++ - 是否有像 C# 中那样的 C++ new 声明

c++ - 将一个大整数与一个数字相乘。我的代码有什么问题?

c++ - 为什么我的 forward_ 函数不适用于右值?

c++ - std::forward 和带有非常量引用参数的构造函数

c++ - g++ 是否不从它存在的第一个包含路径中获取头文件?

c# - _NAME、__NAME、_NAME_、__NAME__ 等符号的含义

c++ - 当其返回类型为 void 时,使用其他 lambda 结果调用 lambda

c++ - C++11 中的 constexpr if-then-else