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
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
Vector { public: ... boost::array 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
Vector { public: Vector(std::initializer_list<double> i) : data{i} {}
... private: boost::array
data; }; Alas, boost::array has no constructor that takes an initializer_list:
error: no matching function for call to ‘boost::array
::array(std::initializer_list<double>&)’ note: candidates are: boost::array ::array(const boost::array &) /usr/local/Boost/1.44/boost/array.hpp:57:17: note: boost::array ::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