c++11 - What does "[&]" mean in C++? -
i reading source code , contains following lines.
auto updateslack = [&](const char slack_type, const double s) { if (slack_type == 'e') { wns.early_slack = min(s, wns.early_slack); tns.early_slack += s; } else if (slack_type == 'l') { wns.late_slack = min(s, wns.late_slack); tns.late_slack += s; } };
i confused "[&]" symbol after "=" operator. can tell suggests?
this statement uses lambda expression.
auto updateslack = [&](const char slack_type, const double s) { if (slack_type == 'e') { wns.early_slack = min(s, wns.early_slack); tns.early_slack += s; } else if (slack_type == 'l') { wns.late_slack = min(s, wns.late_slack); tns.late_slack += s; } };
the compiler 2 things. first 1 compiler defines unnamed class contain function call operator corresponds body of lambda expression.
the second 1 compiler creates object of class , assign variable updateslack
.
you can imagine following way
struct { auto &r1 = wns; auto &r2 = tns; void operator ()( const char slack_type, const double s ) const { if (slack_type == 'e') { wns.early_slack = min(s, wns.early_slack); tns.early_slack += s; } else if (slack_type == 'l') { wns.late_slack = min(s, wns.late_slack); tns.late_slack += s; } } } updateslack;
then can call function call operator of object somewhere in program functional object needed like
updateslack( 'l', 10.10 );
Comments
Post a Comment