Search code examples
c++c++14perfect-forwardingstdbind

Can bind be used to forward a variadic number of arguments?


In C++14, I can write a lambda that does perfect forwarding on an arbitrary number of arguments:

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

Is there a way to achieve the same effect using bind? I know that function objects created by bind take an arbitrary number of arguments, and I know that such objects uses perfect forwarding for unbound arguments, but is there a way to tell bind to create a function object that uses perfect forwarding on every argument passed to it, even if there was no placeholder for it in the call to bind?


Solution

  • It's not currently possible, but see http://cplusplus.github.io/EWG/ewg-complete.html#44 for a suggestion to support it:

    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!