std::array in constructor
How dos one construct an std::array in a constructor initializer sequence?
Code:
myClass
{
public:
myClass()
: array_of_2_ints (1, 2) //<- not working...
{ }
private:
::std::array<int, 2> array_of_2_ints
}
I've tried all permutations of () and {} I could think of...
Re: std::array in constructor
The initializer list is supposed to work but I don't know if gnu has implemented such an experimental feature or not. A constructor initializer sequence is still under debate, is it not? (somebody keep me updated please)
Anyhow, as it stands for now, just default initialize it and use the constructor body, or as it is with the case with c++0x, copy it from the automatic object.
Code:
myClass
{
public:
myClass()
: array_of_2_ints (array<int, 2>{1, 2}) //<- meh...
{
// or do it here
array_of_2_ints.fill(666);
}
private:
::std::array<int, 2> array_of_2_ints;
};
If you look at the boost::array, the whole ctor/ctor/dtor is done through the operator=.
I never used boost array to an extend where I can participate in a debate over it, but the simple notion that arrays can be assigned/copied makes me go "What? you can do that?"
I don't know...
Re: std::array in constructor
According to this documentation there is no appropriate constructor for what you are trying to do. Where did you see the documentation that indicates that there is something other than a default constructor or copy constructor? It all looks consistent to me. You cannot provide values for elements to the constructor. You either use the initializer list or you use an algorithm within the constructor body. Unfortunately for class attributes I don't think that there is a way to use an initializer list for the array unless the array is static.
http://msdn.microsoft.com/en-us/library/bb983093.aspx
http://en.wikipedia.org/wiki/Array_%28C%2B%2B%29
Re: std::array in constructor
Quote:
Originally Posted by
monarch_dodra
How dos one construct an std::array in a constructor initializer sequence?
Code:
myClass
{
public:
myClass()
: array_of_2_ints (1, 2) //<- not working...
{ }
private:
::std::array<int, 2> array_of_2_ints
}
I've tried all permutations of () and {} I could think of...
If I have to init a vector with different values, I normally do something like the following:
Code:
class myClass
{
static int def_array[2];
public:
myClass()
: array_of_2_ints (&def_array[0], &def_array[2]) {}
{ }
private:
::std::array<int, 2> array_of_2_ints
}
// myClass.cpp
int myClass::def_array[2] = { 0, 1 };
I would assume that also works with std::array (which is not available in my current environment).
Re: std::array in constructor
Thanks for your replies. I thought array had some sort of magic that would allow this, and I though the syntax was eluding me.
Guess I'll just initialize it in the constructor, no big deal.