Boost logo

Boost Users :

Subject: [Boost-users] Aggregate a boost::array, initialize with initializer list -- how?
From: Dave Steffen (dave.steffen_at_[hidden])
Date: 2011-01-26 12:11:24


Hi Folks, I've got a question about supporting brace initialization
syntax.

   double a[] { 1,2,3 };

ok, always has been.

   boost::array<int, 3> b {{ 1,2,3 }}

Needs double braces because there's one member data to be initialized,
and we want to initialize that with {1,2,3}. Fine. Some compilers
let you drop one set of braces; GCC 4.4 does, but produces a warning.

Now, I make a class:

   template<size_t N> Vector
   {
   public:
    ...
    boost::array<double, N> data;
   };
   
   Vector<3> v {{{1,2,3}}}

Still OK: again in principle, I need three sets of braces.

However, I'd like my Vector::data member to be private, but as soon as
I do that, Vector isn't a POD any more, and the brace initialization
no longer works. (I'm not sure if this is in line with the standard,
or is a GCC 4.4 limitation.)

No problem, std::initializer_list to the rescue:

   template<size_t N> Vector
   {
   public:

      Vector(std::initializer_list<double> i) : data{i} {}

    ...
   private:
    boost::array<double, N> data;
   };
   
Alas, boost::array has no constructor that takes an initializer_list:

error: no matching function for call to ‘boost::array<double,
3ul>::array(std::initializer_list<double>&)’
note: candidates are: boost::array<double, 3ul>::array(const
boost::array<double, 3ul>&)
/usr/local/Boost/1.44/boost/array.hpp:57:17: note:
boost::array<double, 3ul>::array()
scons: *** [fast/Test/testVector.o] Error 1

Having poked through the standard, I can't see any way to convince the
compiler to turn an initializer_list back into the semantic equivalent
of a brace initializer.

Does anyone know how to do this?

Does boost::array need a constructor that takes initializer_lists?

Thanks!

-- 
Dave Steffen, Ph.D. - Software Engineer
Numerica Corporation <http://www.numerica.us>
4850 Hahns Peak Drive, Suite 200
Loveland, Colorado 80538
main (970) 461-2000 x 227
Email: dave.steffen_at_[hidden]
fax (970) 461-2004

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net