Search code examples
c++c++14eigen

Storing a list of Eigen arrays


I would like to store a collection of Eigen arrays in an std::list or std::vector.

My arrays have different types, some Array<Scalar, 1, Dynamic>, some Array<Scalar, Dynamic, Dynamic>, so I'm not sure what template argument to give std::list.

Ideas:

  • Cast everything to Dynamic and store as std::list<Array<Scalar, Dynamic, Dynamic>>, but this feels like needless copying
  • boost::variant?

For those less familiar with Eigen, Dynamic means -1.

For more context, see here: What's the right type for a join_rows() function?


Solution

  • ArrayXf, ArrayXXd, Array3i, etc. are all different types. Therefore, your question boils down to: Is there a STL container that allows heterogeneous types? The short answer to that is: No. The long answer to that is: No, but...

    There are alternatives. As you pointed out, you could use a list/vector of boost::variant (explicitly list all types) or boost::any (effectively allow any type). You could also use a list/vector<void*> and static_cast at every usage (personally, not my preferred option).