I have a bunch of numerical routines in C++ that take and return
boost::arrays,
for example
array strange_function(array);
in another example, one integrates boost::function that takes an array
of size N and returns arrays of size F
The library is pretty general because it works in any number of
dimensions. But it seems to be a syntactic waste to use those general
functions in the cases where the input or the output is a single
number, i.e. one has to use array all over the place, array
constructions or ugly casts.
One option would be to transform all the functions and boost::function
declaration to take arbitrary combinations of double input/ouput and
array input/out
double strange_function(array);
array strange_function(double );
(actually strange_function is a template function, so what it really
does depends on the template parameter strange_function<T>)
( Think for example of Mathematica functions that take numbers or
vectors Integrate[{f[x],g[x]},x] or Integrate[f[x],x] )
I come up with a solution that is, instead of working with
boost::array work with a similar type that simply believes it is a
number when the dimension is one, here it is the implementation:
template
struct array : boost::array{
typedef boost::array base;
array(base const& arr) : base(arr){}
array(double const& d);
operator double&();
operator double const&() const;
};
template<> array<1>::array(double const& d) : base((base){{d}}){}
template<> array<1>::operator double&(){return (*this)[0];}
template<> array<1>::operator double const&() const{return (*this)
[0];}
in this way the array is transformed from and into a number if
requested, and the existing code that uses boost::array still works.
Just wondering for some feedback.
Thank you,
Alfredo