Skip to content

Commit

Permalink
tst_ContainerApiSymmetry: fix mutable lambda anti-pattern
Browse files Browse the repository at this point in the history
STL algorithms, in general, don't specify how often the function
objects passed to them are copied during the run of the
algorithm.

While generate_n is above any reasonable suspicion of copying the
function object after the first invocation, passing a mutable lambda
containing the counter is still an anti-pattern we don't want people
to copy.

Fix in the usual way, by keeping the counter external to the lambda.

As a drive-by, replace post- with pre-increment.

Amends dc091e7.

Pick-to: 6.5 6.2
Change-Id: I9c44e769fd41e5f7157179a2be4c3534424cf913
Reviewed-by: Ivan Solovev <[email protected]>
  • Loading branch information
marcmutz committed May 13, 2023
1 parent 3cc3919 commit edc9539
Showing 1 changed file with 2 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,8 @@ Container make(int size)
Container c;
c.reserve(size);
using V = typename Container::value_type;
std::generate_n(std::inserter(c, c.end()), size, [i = 1]() mutable { return V(i++); });
int i = 0;
std::generate_n(std::inserter(c, c.end()), size, [&i] { return V(++i); });
return c;
}

Expand Down

0 comments on commit edc9539

Please sign in to comment.