eigen auto type deduction in general product

后端 未结 2 1733
时光取名叫无心
时光取名叫无心 2020-12-21 04:08

I have the following piece of code (I apologize for the slightly larger code snippet, this is the minimal example I was able to reduce my problem to):

#inclu         


        
相关标签:
2条回答
  • 2020-12-21 04:23

    It probably has a lazy evaluation type that is only safe to evaluate once. You could capture it with:

    auto autoresultmatrix = autoresult.eval()
    
    0 讨论(0)
  • 2020-12-21 04:35

    The problem is that Id() returns a temporary which is stored by reference in the object representing the expression Id(Foo, 4) * v. Thus after the auto statement, autoresult stores a reference to a dead object. If you do not want an abstract expression but the actual result, do not use auto or call eval to enforce evaluation:

    auto autoresult = (Id(Foo, 4) * v).eval();
    

    A third option is to make the object returned by Id() available for further computations:

    auto id4 = Id(Foo,4);
    auto autoresult = id4 * v;
    

    but in this case, anytime you use autoresult then the product will be re-evaluated and the following will output different results:

    cout << autoresult;
    v.setRandom();
    cout << autoresult;
    
    0 讨论(0)
提交回复
热议问题