[eigen] Value-initializing a Matring
• To: eigen@xxxxxxxxxxxxxxxxxxx
• Subject: [eigen] Value-initializing a Matring
• From: Matan Nassau <matan.nassau@xxxxxxxxx>
• Date: Sat, 18 Apr 2015 17:22:14 +0000
• Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=qHKXtJiR1COJ5JfZBptYeYLG+uLjUZ1IhW+57qVBlz4=; b=SL8gOid1tDLPrUNHcIpWJVHHIff9YxP7MrSe6izD/VVuRsQOqO98NAsVgUIs3JFU+3 F9hD8n5+0H46IDqHvYrSDQ9igcsTs6jF+pyySq6/57haK63fiR19DunKDGBeyi5mD0Gr 7nyepAauWQm9dCVC7JKSNOCigjKAtITQx8x3hv41kZ9r7gjK4XakskufdPSRDb76Gmy7 PsljxIOjk47eBiVI6IOKOcOMaL+dpxGiVJlzfhTzKHyUHZxGj/wO5ojAsYIKqI+1d9EN o8O7dJ2OSE8KVuJ2TM59ncnYbR86LNXn4B7S5lO0eqyqXONS5BCQLB0nTrTypdvv+krY G3FQ==

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;
// ...