It\'s pretty well known that the default behaviour of std::bind and std::thread is that it will copy (or move) the arguments passed to it, and to use reference semantics we will
For both std::bind
and std::thread
, the invocation of the function on the given arguments is deferred from the call site. In both cases, exactly when the function will be called is simply unknown.
To forward the parameters directly in such a case would require storing references. Which may mean storing references to stack objects. Which may not exist when the call is actually executed.
Oops.
Lambdas can do it because you're given the ability to decide, on a per-capture basis, whether you want to capture by reference or value. Using std::ref
, you can bind a parameter by reference.