Std :: forward of rvalue ref to lambda?

Consider the following two fragments:

Exhibit A :

template<typename CalcFuncT> int perform_calc(CalcFuncT&& calcfunc) { precalc(); int const calc = calcfunc(); postcalc(); return calc; } int main() { perform_calc([]{ return 5 * foobar_x() + 3; }); // toFuture perform_calc([]{ return 5 * foobar_y() - 9; }); // toPast } 

Figure B :

 template<typename CalcFuncT> int perform_calc(CalcFuncT&& calcfunc) { precalc(); int const calc = std::forward<CalcFuncT>(calcfunc)(); postcalc(); return calc; } int main() { perform_calc([]{ return 5 * foobar_x() + 3; }); // toFuture perform_calc([]{ return 5 * foobar_y() - 9; }); // toPast } 

Diff

  precalc(); - int const calc = calcfunc(); + int const calc = std::forward<CalcFuncT>(calcfunc)(); postcalc(); 

What is the difference (if any) between the generated code of these two code fragments?

In other words, what is the effect of std :: forward mentioned above, if any?

Note that this question does not ask what std :: forward does at all - only what does it do in the above context?

+4
source share
1 answer

forward translates the lambda object to x before calling the operator () () on it. The lambda object operator () () is not qualified or overloaded with "& &" and therefore forward should not have any effect.

+4
source

Source: https://habr.com/ru/post/1404026/


All Articles