Boost logo

Boost :

From: christopher diggins (cdiggins_at_[hidden])
Date: 2005-05-31 11:48:24


The fact that the Boost array class does not support constructors in favour
of the limited and somewhat obscure aggregate initialization syntax has me
concerned. IMO it would be much more flexible and powerful to support
overloading of the comma operator. The following works on Visual C++

  template<typename array_T, unsigned int N>
  struct array_comma_helper
  {
    array_comma_helper(array_T& x) : m(x) { }
    array_comma_helper(const array_comma_helper& x) : m(x.m) { }
    array_comma_helper& operator=(typename array_T::value_type x) { m[0] =
x; return *this; }
    mutable array_T& m;
  };

  template<class array_T, unsigned int N, class Scalar_T>
  array_comma_helper<array_T, N+1>
  operator,(array_comma_helper<array_T, N> h, const Scalar_T& x) {
    array_comma_helper<array_T, N+1> ret(h.m);
    ret.m[N + 1] = x;
    return ret;
  }

  template<class array_T>
  array_comma_helper<array_T, 0>
  initialize(array_T& x) {
    return array_comma_helper<array_T, 0>(x);
  }

This enables us to write:

    int n = 2;
    array<int, 3> a;
    initialize(a) = 1, n, 3;

This is much more flexible and it allows us to have constructors in
boost::array thus making it a full reversible container.

Christopher Diggins
http://www.cdiggins.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk