Re: [eigen] Value-initializing a Matring

[ Thread Index | Date Index | More Archives ]

But, there is no initializer list here. There is an =default, yes, but maybe we can do that conditionally?

#if __cplusplus >= 201103L

If Eigen supports C++11 initialization semantics it opens the door for more performant code, and for more expressive flexibility in user code. I mean, I can clearly express what I want with

Matrix3d m;


Matrix3d m{};

controlling the performance or behaviour in case-by-case fashion, in my code.

Eigen can do this, while still providing the current behaviour for C++03.
On Sat, Apr 18, 2015 at 15:47 Gael Guennebaud <gael.guennebaud@xxxxxxxxx> wrote:

Eigen aims to be compatible with C++03, and initializer-lists are not supported yet. So currently Vector2d v{}; is equivalent to Vector2d v; .

See this bug entry for further discussions on initializer-lists: They will likely be supported in Eigen 3.3, and we already agreed on making the empty initializer-list initializes to 0.

In the meantime, you can compile with -DEIGEN_INITIALIZE_MATRICES_BY_ZERO, as documented there:


On Sat, Apr 18, 2015 at 7:22 PM, Matan Nassau <matan.nassau@xxxxxxxxx> wrote:
I have a class,

 struct foo {
   foo() : v{} {}
   Eigen::Vector2d v;

I just got bitten when I realized the value of v is undetermined.
I understand the default constructor of a fixed-size matrix does nothing. In particular,

 Eigen::Vector2d v{};

will create a vector with an undetermined value.

Why is that? Is this for speed?

If I value-initialize an object I expect it to initialize. To motivate, all standard templates and classes behave this way:

 std::string s{};  // assert(s=="");
 std::vector v{};  // assert(v.size() == 0);


 std::string s;  // assert(s=="");

but if we want speed here then we can do

 struct Matrix {
   Matrix() = default;
   // ...

This way, we'd get the best of both worlds:

 Eigen::Vector2d v;  // valid, undetermined value
 Eigen::Vector2d v{};  // assert(v==Eigen::Vector2d::Zero());


Mail converted by MHonArc 2.6.19+