Boost.Array intialisation

Hello all, the documentation describes that Boost.Array deliberately omits constructors to allow intialisation in functions: void F() { boost::array<int, 4> a2 = { 1, 2, 3 }; } That's fine, but how does one intialise a Boost.Array in member intialiser list, e.g.: struct Foo { Foo() : m_a(???) { } boost::array<int, 4> m_a; }; Ofc initialisation can be done in the body of the constructor, but this does not work for const member variables. Also above design choice is contradictionary with STL containers, which gets default initialised. (e.g. m_v (3); ofc there is a difference, since vectors have a dynamic size). thx in advance

2010/3/10 gast128 <gast128@hotmail.com>
That's fine, but how does one intialise a Boost.Array in member intialiser list
boost::array<int, 4> MakeArray() { boost::array<int, 4> res = { 1, 2, 3, 4 }; return res; } struct Foo { Foo() : m_a(MakeArray()) {} boost::array<int, 4> m_a; }; Roman Perepelitsa.

Or use boost assign as shown inline below. Though I'm not sure of the performance differences. Roman Perepelitsa wrote:
2010/3/10 gast128 <gast128@hotmail.com <mailto:gast128@hotmail.com>>
That's fine, but how does one intialise a Boost.Array in member intialiser list
boost::array<int, 4> MakeArray() { boost::array<int, 4> res = { 1, 2, 3, 4 }; return res; }
#include <boost/assign/list_of.hpp> #include <boost/array.hpp> using namespace boost::assign;
struct Foo { Foo() : m_a(MakeArray()) {}
Foo() : m_a(list_of(1)(2)(3)(4)) {}
boost::array<int, 4> m_a; };
Roman Perepelitsa.
Jeff

#include <boost/assign/list_of.hpp> #include <boost/array.hpp>
using namespace boost::assign;
struct Foo { Foo() : m_a(list_of(1)(2)(3)(4)) {}
boost::array<int, 4> m_a; };
Roman Perepelitsa.
Jeff
Thx this works. I was more hoping for a solution without a helper function (e.g. m_a({1, 2}), but that doesn't compile), but probably it doesn't exist. Got btw also a C4701 warning by uisng this construct. Haven't delved into it to see if it is a dangerous one.

That's fine, but how does one intialise a Boost.Array in member intialiser list, e.g.:
The only way I found to do this is #include<boost/array.hpp> struct Foo { Foo() : m_a((boost::array<int, 4>){1,2,3,4}) { } boost::array<int, 4> m_a; }; I am using this kind of construction often now because it is the cleanest and the most explicit that I can get but I don't know the degree of portability or even correctness of such code. For example I know that it works in GCC 4.1.2, GCC 4.4 and IntelCC 11 but it gives an error with gcc option -pedantic-errors (error: ISO C++ forbids compound-literals). Despite of this possible issue I am using it anyway, but I also would like to know what are the possible issues and portability (please, let me know if you compiler is not gcc). Alfredo

alfC <alfredo.correa <at> gmail.com> writes:
#include<boost/array.hpp> struct Foo { Foo() : m_a((boost::array<int, 4>){1,2,3,4}) { } boost::array<int, 4> m_a; };
Despite of this possible issue I am using it anyway, but I also would like to know what are the possible issues and portability (please, let me know if you compiler is not gcc).
This doens't compile on vstudio 2003. I have to put here some extra dummy text, because without this text i get 'There's much more quoted text in your article than new. Prune quoted stuff.'.
participants (4)
-
alfC
-
gast128
-
Jeff Flinn
-
Roman Perepelitsa