Aggregate a boost::array, initialize with initializer list -- how?
Hi Folks, I've got a question about supporting brace initialization
syntax.
double a[] { 1,2,3 };
ok, always has been.
boost::array
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
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
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
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.
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@numerica.us 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.
participants (2)
-
Dave Steffen
-
michi7x7