Avoid dangling references in lambdas returned by parameterization interface
Overview
The following discussion from !187 (merged) should be addressed:
-
@sospinar started a discussion: (+1 comment) I have to accept that I never realized of this
this
on the parameterization interface. And now that I do, I do not like it at all. This lambda is getting passed through at least two classes with these references and is shouting "I will hold a dangling reference whenever you don't expect it". It is never advised to export lambdas with closures that hold references to other scopes (see CppCoreGuidelines or Effective Modern C++ ).Now is perhaps too late to complain and ask for a redesign as we have several of them, so I would be pleased with just a big warning to use these functions and those in the parameter classes: "only if you know what you are doing" or with very precise instructions how to avoid the dangling references.
Proposal
In the lambdas returned by Dorie::Parameterization::Richards
and ::MualemVanGenuchten
, capture the required parameters by value. Edit: Parameterizations in the Transport model are affected as well.
How to test the implementation?
Evaluate a parameterization function after destroying the parameterization interface which supplied it.