Boost logo

Boost Users :

Subject: Re: [Boost-users] Aggregate a boost::array, initialize with initializer list -- how?
From: michi7x7 (mailing-lists_at_[hidden])
Date: 2011-01-26 12:29:28


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.
>
> 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.

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

Regards,

michi7x7


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