How to add a user-defined typecast for a variable in c++?

62 Views Asked by At

I have a class with functions that take vector<double> as input and also return vector<double>.

I want to overload this function to take Eigen::vectorXd as input and output. I have many functions that need to be overloaded. Is there a way to create a typecast which would typecast Eigen::vecrtorXd to vector<double> for all these functions?

1

There are 1 best solutions below

2
Jack Zhou On

Depending on what you're looking for, you might be able to achieve this with a basic templated function, i.e.

template <typename vec_t>
vec_t do_something(const vec_t& input)
{
  // Do things
}

This works fine if you want to do something generic like calculate a norm where the dimension of the vector doesn't matter.