Boost logo

Boost Users :

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


On Wednesday, January 26, 2011 10:29:28 am michi7x7 wrote:
> Am 26.01.2011 18:11, schrieb Dave Steffen:
> > 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.

  ... and BTW, GCC 4.5.1 does the same.

> >
> > 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!
>
> Hi,
>
> Well, initializer_lists are a C++0x-feature, so is std::array<>. Maybe
> you should switch from boost::array to std::array.

  I tried that -- std::array has the same issues (at least, the impls
  in GCC 4.4 and 4.5 do).

> Another solution would be to use std::copy like so:
> std::copy(init.begin(), init.end(), data.begin());

  Yeah, clearly we could loop over the initializer_list and copy into
  the boost::array... I'm not sure what that would to do runtime
  performance, though -- you'd be relying on the compiler to unroll a
  compile-time-sized loop (which is generally doable). Or maybe a
  memcpy from the initializer_list -- are initializer_lists required to
  be contiguous?

  Plus, you're no longer constructing the array in the constructor's
  initialization list, but rather in the constructor body, which
  in general may produce other problems.

  I'll look into the performance overhead, though. 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   direct (970) 612-2327
Email: dave.steffen_at_[hidden]
fax (970) 461-2004
This message and any attachments are intended only for the individual
or entity to which the message is addressed.  This is a private
message and may contain privileged information.  If you are neither
the intended recipient nor the agent responsible for delivering the
message to the intended recipient, you are hereby notified that any
review, retransmission, dissemination, or taking of any action in
reliance upon, the information in this communication is strictly
prohibited, and may be unlawful.  If you feel you have received this
communication in error, please notify me immediately by returning this
email to me and deleting it from your computer.

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