Is there a way to populate a std::array
in a class' constructor initializer?
Right now I'm populating it the long way:
class Matrix3x3 {
//...
private:
std::array<double, 9> _indicies;
};
//...
Matrix3x3::Matrix3x3(double m00, double m01, double m02, double m10, double m11, double m12, double m20, double m21, double m22) : _indicies() {
_indicies[0] = m00; _indicies[1] = m01; _indicies[2] = m02;
_indicies[3] = m10; _indicies[4] = m11; _indicies[5] = m12;
_indicies[6] = m20; _indicies[7] = m21; _indicies[8] = m22;
}
-EDIT--
Visual Studio 2010 does not have full c++11 support (it still lists std::array as a typedef to the std::tr1::array
namespace!).
Attempts include:
1. Matrix3x3::Matrix3x3(const std::initializer_list<std::array<double, 9> >& row_column_values) : _indicies(row_column_values) { }
2. Matrix3x3::Matrix3x3(double m00, double m01, double m02, double m10, double m11, double m12, double m20, double m21, double m22) : _indicies({m00, m01, m02, m10, m11, m12, m20, m21, m22}) { }
None of which work in VS2010. All of them report errors:
1. error C2664: 'std::tr1::array<_Ty,_Size>::array(const std::tr1::array<_Ty,_Size> &)' : cannot convert parameter 1 from 'const std::initializer_list<_Elem>' to 'const std::tr1::array<_Ty,_Size> &'
2. Cascading errors:
error C2059: syntax error : ')'
error C2143: syntax error : missing ')' before '{'
error C2143: syntax error : missing ';' before '}'
error C2447: '{' : missing function header (old-style formal list?)
In C++11 you can use a curly brace initializer, like this:
Matrix3x3::Matrix3x3(double m00, double m01, double m02, double m10, double m11, double m12, double m20, double m21, double m22)
: _indicies({m00, m01, m02, m10, m11, m12, m20, m21, m22}) {
}
Demo on ideone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With