Use lambda to access class members within a member function, you need to write this . [this] is required in the following code:
void handleThings() { std::thread myThread([this]() { handler(); }); }
You can write this by reference, but this is not as efficient as capturing by value. because it takes a double indirect to go through the link (optimization modulo the compiler)
void handleThings() { std::thread myThread([&]() { handler(); }); }
Lambdas are usually a better choice than bind .
- Itβs easier for readers to understand.
- More efficient.
source share