(Edit: this certainly does not explain the ICE; I read the original question too hastily.)
The One problem in that code is that the lambdas returned from the adder
function contain dangling references to the x
variable that no longer exists. Capture by copy ([=]
or [i]
) instead of a reference ([&]
) and everything should work.