Suppose I have a function like this:
vector<double> compute(const vector<double>& x, vector<double> dx = boost::none) const{
if (dx)
dx = getDerivative();
// do operations on x and return result
}
and I have two use cases for this function, one where I just need the result of the computation, and another where I need the result and the derivative, e.g.:
vector<double> J;
vector<double> y = compute(x);
vector<double> y = compute(x, J);
When I call the second version (passing in J), the values of J are not updated, though dx
gets updated inside compute
. I would guess this is an issue regarding a pass by reference, but when I update the function signature to vector<double>& dx
, I get the following error:
error: cannot bind non-const lvalue reference of type ‘vector<double>&’ to an rvalue of type ‘vector<double>’
Any suggestions how I can tackle this?