Boost logo

Boost :

From: Sylvain Pion (Sylvain.Pion_at_[hidden])
Date: 2006-08-08 18:18:48


I would like to use boost::array as a class data member.

This raises a problem when constructing it in the
initialization list of the class members:

class A {
   boost::array<int, 3> a;
   A()
     : a(???) {} // what do I write here ???
};

Is there a way to construct the data member efficiently?
By efficiently, I mean using copy constructors instead of
default constructors followed by assignments of array elements.

I have not found anything in the documentation on this question,
and the only initialization method mentioned there is the ={...}
syntax, which does not work in this case. Did I miss anything ?

The way I managed to do what I wanted was to introduce an
auxiliary function:

   template < typename T > inline
   boost::array<T, 3>
   make_array(const T& b1, const T& b2, const T& b3)
   {
     boost::array<T, 3> a = { b1, b2, b3 };
     return a;
   }

and using it:

   class A {
     boost::array<int, 3> a;
     A()
       : a(make_array(1,2,3)) {}
   };

With a compiler doing the return value optimization, this does what
I need.

Do people think it is an important enough use case, that this would
warrant to be added to Boost.Array ? What about TR1's array ?

Of course, it would require N overloads of the make_array()
functions (unless variadic templates are used :). But at least
providing it for e.g. N<10 in boost would be useful, IMHO.

-- 
Sylvain

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