capturing perfectly-forwarded objects in lambdas
11 december 2016
Perfect forwarding and forwarding references allow developers to write generic template functions that retain the lvalueness/rvalueness of passed arguments, in order to avoid unnecessary copies or support reference semantics without having to implement multiple overloads. (This article by Eli Bendersky explains them in depth. I will assume you’re familiar with these concepts for the rest of the article.)
In this article, I’ll show how to correctly capture perfectly-forwarded objects into lambdas:
We’ll take a look at an example that shows how using
std::forward
in lambda captures can produce unexpected results.