std::thread constructor Is there a difference between passing a pointer and passing by ref?
No, there are no differences, but note that using a reference wrapper has only become possible with the acceptance of LWG 2219 as a defect report at the Oct 2015 WG21 meeting.*
Using std::ref
may help in cases where you have a named object instance rather than this
, since this
is quite easy to spell. But consider the following situation, in which you'd like to stay nicely const-correct:
A a;std::thread(&A::foo, std::cref(a), 1, 2);
This may be easier to read than:
std::thread(&A::foo, &(const_cast<const A&>(a)), 1, 2);std::thread(&A::foo, &as_const(a), 1, 2);std::thread(&A::foo, const_cast<const A*>(&a), 1, 2);
*) Vendors that keep distinct language dialects around, like GCC's and Clang with the -std
flag), will typically consider defects to apply to all dialects and "fix" the implementations. Defects are things that "were always meant to be the way we say now".