0

Is there a way to create an array of ublas c_vectors with different sizes?

For example

array[0] would return an ublas::c_vector< double, 3 > (size=3) and array[0](0) would access its first element

array[1] would return an ublas::c_vector< double, 7 > (size=7) and array[1](0) would access its first element

etc

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
niels
  • 760
  • 8
  • 25

1 Answers1

0

I think you can use std::vector<boost::any>, and then push ublas::c_vector of different sizes into it.

std::vector<boost::any> v;
v.push_back(ublas::c_vector<double,3>());
v.push_back(ublas::c_vector<double,7>());
v.push_back(ublas::c_vector<double,9>());
//etc

Elements should be cast back to the appropriate types, using boost::any_cast which is custom keyword cast for extracting a value of a given type from boost::any.

You could try boost::variant as well. Choose whatever suits your need better. Read this before making a decision:

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • That seems to be the only solution, given that vectors of different sizes don't share any common base class. – Kerrek SB Sep 19 '11 at 20:25
  • I tried this but a get a "call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function" Also tried this 'boost::array v = {ublas::c_vector(), ublas::c_vector()};' and got the same error – niels Sep 19 '11 at 20:27
  • @KerrekSB: One could use `Variant` as well. But I don't know the exact requirement. – Nawaz Sep 19 '11 at 20:27
  • @niels: With boost.any, you have to cast the elements back to the required type. I think it's `any_cast` or something like that, but I'm not sure. – Kerrek SB Sep 19 '11 at 21:10
  • @Kerrek : It is `any_cast`. :-) – Nawaz Sep 20 '11 at 04:52