I have a function that currently accepts 2 vectors that can contain any plain old data ...
template <class T>
void addData(const vector<T>& yData, vector<T> xData)
{ .. }
Question:
- Would it be possible to modify it to take two
std::array
or twostd::vector
, or even a combination thereof, given that these containers take a different number of template arguments?
Sure, it's just a matter of creating a suitable type trait. The example just uses a function
f()
with one argument but it is trivial to extend to take any number of arguments.An alternative solution:
Why not just use this, which works with any container using random-access iterators, including plain old arrays. If you can use iteration instead of indexing, you can do away with the random-access requirement as well.
C++03 version (doesn't support plain old arrays):