Re: [eigen] Value-initializing a Matring |

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

*To*: eigen <eigen@xxxxxxxxxxxxxxxxxxx>*Subject*: Re: [eigen] Value-initializing a Matring*From*: Gael Guennebaud <gael.guennebaud@xxxxxxxxx>*Date*: Mon, 20 Apr 2015 13:39:44 +0200*Dkim-signature*: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=kyHWS/zE+LKRfe27rNwFS0MTeLaPuommgMOYjTGTtnM=; b=N4s20XyavvDy+kYG6OctZ4u78lsfzg6MBc7D4ROnQoDrXCOW8ni6L4bN5ZPOkzxhHL i9t4pvz//E7E0BwwJ9raGjJSaeA55Sbj0Rp1waKX4CtXsmPjI1o568spXb374KHkeO16 zGjzwPHDsaWCHLDrr6OTnRYKssHAJCMVXAHfakwAw+Mf2prR4kQmHRyUqa70cWh6Drd1 XEFYraHwRPakA9L+IWxyOn3Vjro3vpR+UyhpFpAkU8kzKwvk1tA4esiSP9lLLx3bvXqW MASHo9L+NnH4dYdD1XUxuzjV5REG7ZRaQetqOA/pCB4iacJRv9KnEQvw/7/SdjB/DoWu qWRg==

On Sun, Apr 19, 2015 at 12:26 AM, Matan Nassau <matan.nassau@xxxxxxxxx> wrote:

But, there is no initializer list here.

right, but without C++11, as soon as we need other ctors, we cannot distinguish between a default and value initialization. So using Matrix()=default in c++11 would lead to very different and unsafe behaviors between C++03 and C++11. This is why, IMO, initializer lists is the right solution to this problem. And of course, as Christoph said, using Matrix::Zero() is what you should do in the meantime, and/or to be compatible with C++03.

gael

There is an =default, yes, but maybe we can do that conditionally?

#if __cplusplus >= 201103L

Matrix()=default;

#else

#ifdef EIGEN_INITIALIZE_MATRICES_BY_ZERO

...

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;

and

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:Hi,

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: http://eigen.tuxfamily.org/bz/show_bug.cgi?id=954. 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: http://eigen.tuxfamily.org/dox/TopicPreprocessorDirectives.html#title0.cheers,gaelOn Sat, Apr 18, 2015 at 7:22 PM, Matan Nassau <matan.nassau@xxxxxxxxx> wrote:will create a vector with an undetermined value.Eigen::Vector2d v{};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,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 dotemplate<...struct Matrix {Matrix() = default;

// ...This way, we'd get the best of both worlds:Eigen::Vector2d v; // valid, undetermined valueEigen::Vector2d v{}; // assert(v==Eigen::Vector2d::Zero());Matan

**Follow-Ups**:**[eigen] binder2nd deprecated***From:*Gabriel

**References**:**[eigen] Value-initializing a Matring***From:*Matan Nassau

**Re: [eigen] Value-initializing a Matring***From:*Gael Guennebaud

**Re: [eigen] Value-initializing a Matring***From:*Matan Nassau

**Messages sorted by:**[ date | thread ]- Prev by Date:
**Re: [eigen] Value-initializing a Matring** - Next by Date:
**[eigen] Colorized bugzilla buglists** - Previous by thread:
**Re: [eigen] Value-initializing a Matring** - Next by thread:
**[eigen] binder2nd deprecated**

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