
Dear all, In using boost::array I have felt the need to pass around boost::array's of an unknown size (unknown to the function receiving the array). I have therefore created an abstract base class for boost::array so that I can create a pointer to a common base class for arrays of different sizes. Essentially std::vector's of different sizes can be passed into a function with a fixed prototype, but because boost::array constructs a different type for each array size, a single fixed prototype cannot be described such that it can be called with different sizes of boost::array. In code I can write (toy functions) : double sumTotal(const std::vector<double>* values) { double result = 0 ; BOOST_FOREACH(double v, values) { result += v; } return result; } But I can't write: double sumTotal(const boost::array<double>* values) { double result = 0 ; BOOST_FOREACH(double v, values) { result += v; } return result; } Without the second function being a template function, i.e. I cannot provide a library that takes a boost::array<double>. With the attached base class, I could define a function that takes a boost::array_base<double>. Since, I think, the ability to allow boost::array to be used in this way is that it requires the use of virtual functions, I have written it so that the inheritance is optional. It is enabled with a #define before including array.hpp . Is this useful? (I am aware that I maybe could use a boost::range) Thanks for your time, Peter Ps. On my current compiler, swap and operator= could not be uncommented in the array_base class. PPs. This is my first post to boost dev, please be kind... J Dr Peter Myerscough-Jackopson - Senior Engineer Tel: +44 (0)23 8076 7808 Fax: +44 (0)23 8076 0602 Web: http://www.macltd.com/ <BLOCKED::http://www.macltd.com/> Email: peter.myerscough-jackopson@macltd.com <mailto:peter.myerscough-jackopson@macltd.com> MULTIPLE ACCESS COMMUNICATIONS LIMITED is a company registered in England at Delta House, The University of Southampton Science Park, Southampton, SO16 7NS, United Kingdom with Company Number 1979185 and VAT Number GB 411942866

AMDG Peter Myerscough-Jackopson wrote:
In using boost::array I have felt the need to pass around boost::array's of an unknown size (unknown to the function receiving the array). I have therefore created an abstract base class for boost::array so that I can create a pointer to a common base class for arrays of different sizes.
This is a non-starter, because it means that boost::array will no longer be an aggregate. In Christ, Steven Watanabe

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Monday 15 March 2010, Peter Myerscough-Jackopson wrote:
In using boost::array I have felt the need to pass around boost::array's of an unknown size (unknown to the function receiving the array). I have therefore created an abstract base class for boost::array
It wouldn't be an aggregate then: http://www.boost.org/doc/libs/1_42_0/doc/html/array/rationale.html -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkuekjEACgkQ5vihyNWuA4XVIACfVRgKyvwP7JrHjvv1sq0/MPkz gAsAoMOExHMkJSecnPeit2a5bbNN/tRq =0E1B -----END PGP SIGNATURE-----

Peter Myerscough-Jackopson wrote:
Dear all,
In using boost::array I have felt the need to pass around boost::array's of an unknown size (unknown to the function receiving the array). I have therefore created an abstract base class for boost::array so that I can create a pointer to a common base class for arrays of different sizes. [...]
Have you read the Boost.Array design rationale? http://www.boost.org/doc/libs/1_42_0/doc/html/array/rationale.html In any case, I'd imagine an abstract base class for boost::array would not be well-received. I consider boost::array as basically just an as-simple-as possible "copy constructible C-array". Anything more complex is something different than boost::array. If you want a uniform, size-independent interface for receiving boost::array parameters, you can use something like the following: template< class It > void foo(boost::iterator_range< It > r) { ... } template< class T, std::size_t N > void foo(boost::array<T,N>& a) { foo(boost::make_iterator_range(a)); } See the Boost.Range documentation: http://www.boost.org/doc/libs/1_42_0/libs/range/doc/utility_class.html#iter_...
Since, I think, the ability to allow boost::array to be used in this way is that it requires the use of virtual functions, I have written it so that the inheritance is optional. It is enabled with a #define before including array.hpp .
Using the preprocessor to achieve a policy decision such as this is probably less preferable compared to using a template parameter. - Jeff
participants (5)
-
Frank Mori Hess
-
Jeffrey Hellrung
-
Mathias Gaunard
-
Peter Myerscough-Jackopson
-
Steven Watanabe