[eigen] Value-initializing a Matring

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


I have a class,

 struct foo {
   foo() : v{} {}
   private:
   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);

Granted,

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

but if we want speed here then we can do

 template<...
 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());


Matan


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/