Re: [eigen] C++11 Style Initialisation

[ Thread Index | Date Index | More Archives ]


this would be a very welcome addition. It seems to me that bug 954 is exactly about that (though the initial request is slightly different). Regarding c++11, it is sufficient to guard the additional c++11 code with:


or something more specific about initializer_lists and/or variadic templates if needed.

David's addons could be a starting point that should be extended:
 - implemented as a ctor
 - asserts if the sizes don't match
 - support 1D vectors
 - support dynamic-size vectors and matrices: sizes must be extracted from the initializer lists.
 - make sure broadcasting for fixed size arrays still work: Array4f twos{2.f}; Array44f fours{4.f};

To be implemented we'll have to deal with the tricky ambiguous cases of initialing a runtime vector or matrix with either 1 or 2 elements. (we've already dealt with the compile-time versions)

Ideally, we would like to be able to write:

   VectorXd v{1,2,3,4,5,6};
   MatrixXd v{1,2,3,4}; // init a 1x4 matrix

but as mentioned by Daniel, we'll get troubles with:

  VectorXd{3}; -> non initialized 3x1 or 1x1 [3]?
  VectorXd{3,1}; -> non initialized 3x1 or 1x1 [3,1]^T?
  MatrixXd{3,2}; -> non initialized 3x2 or 1x2 [3,2]?

Possible solutions:
 - enforce the usage of a tagged ctor (as suggested by Daniel)
 - use a static member
 - enforce usage of double braces:
 VectorXd{{3}}; -> [3]
 VectorXd{{3,1}}; -> [3,1]^T
 MatrixXd{{3,2}}; -> [3,2]

If we go with the always double brace approach, we would enable implicit transposition for column vectors:

VectorXd{{3,1}}  <=> VectorXd{{3},{1}};


On Tue, Jan 15, 2019 at 1:55 PM Wood, Tobias <tobias.wood@xxxxxxxxx> wrote:



I would very much like to write expressions like:


Eigen::Matrix3f example{1, 2, 3, 4, 5, 6, 7, 8, 9};


I have tried searching, but I cannot see a current Bugzilla entry for functionality like this. I found, but that’s slightly different. I can think of several use cases for this, and there are similar StackOverflow questions ( and


I know that Eigen is committed to supporting C++03. Is it possible to add support for this in unsupported/cxx11? I would be tempted to try adding this myself, but I would need some pointers on where to start.




Eigen::Vector3f example2{1, 2, 3};


does work, similarly Vector2f and Vector4f, because they have specific constructors that allow you to write


Eigen::Vector3f example(1, 2, 3);


however, these do not extend to the general case.


Best wishes,


Mail converted by MHonArc 2.6.19+