I'm writing a function that has 1 input and 3 outputs like the following:
void ComputeABC(const Eigen::Vector2d& x,
Eigen::Matrix2d& a,
Eigen::Matrix2d& b,
Eigen::Matrix2d& c)
However, I need my output types to be compatible with both Eigen::Matrix2d
and Eigen::Map<Eigen::Matrix2d>
. Luckily, Eigen provides a Ref
type:
void ComputeABC(const Eigen::Vector2d& x,
Eigen::Ref<Eigen::Matrix2d> a,
Eigen::Ref<Eigen::Matrix2d> b,
Eigen::Ref<Eigen::Matrix2d> c)
Now comes the tricky part. a, b, and c are all expensive to compute, but some intermediate values can be shared in the computation, thus saving some compute. Given that these are expensive, I want to optionally compute each of these. I can do this by making each output type of pointer, and pass in NULL
to signal that I don't want to compute that specific value.
void ComputeABC(const Eigen::Vector2d& x,
Eigen::Ref<Eigen::Matrix2d>* optional_a,
Eigen::Ref<Eigen::Matrix2d>* optional_b,
Eigen::Ref<Eigen::Matrix2d>* optional_c)
Unfortunately, this is quite ugly, since the user now has to construct a Ref
and then pass it in. Trying to pass in an Eigen::Matrix2d*
or Eigen::Map<Eigen::Matrix2d>*
will result in a compile error.
Does anyone have any suggestions for how to make this function easier to use, given the following criteria?
- Adding an additional 3 bools to optionally compute values is quite clunky, and the user will still have to construct a dummy
Eigen::Ref<Eigen::Matrix2d>
for each unwanted output. - The calling code has either a
Eigen::Matrix2d
orEigen::Map<Eigen::Matrix2d>
for each arg that needs to be populated, preferably with zero copies. - Avoid using bare
double*
arrays as, these don't provide any bounds checking on the memory being used. - Any subset of a, b, & c can be requested. ( [a], [a, b], [a, b, c], [b], [b, c], [c] ). Thus, overloads don't scale well.