boostorg / yap

A C++14-and-later expression template library
https://boostorg.github.io/yap
Boost Software License 1.0
108 stars 28 forks source link

why two evaluation results are different? #101

Closed jojogh closed 4 years ago

jojogh commented 4 years ago

I want to use boost yap to do some small example, the codes are below, why I put the expression into an operator or member function in a structure, the two evaluation results are different? I am looking forward to your reply, many thanks! new4.cpp.txt

jojogh commented 4 years ago

seems no one would answer my question.

greole commented 4 years ago

It seems like I experience a similar issue when using stateful terminals created from rvalue references. I'll have look if I can figure out what is going on...

jojogh commented 4 years ago

many thanks

greole notifications@github.com 于2020年2月17日周一 上午1:19写道:

It seems like I experience a similar issue when using stateful terminals created from rvalue references. I'll have look if I can figure out what is going on...

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/boostorg/yap/issues/101?email_source=notifications&email_token=AABCZJLIS5YJYPMJEWBPOO3RDFYRBA5CNFSM4KPR2LNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEL4MYTI#issuecomment-586730573, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABCZJOHJL22ARBCIMEGNFTRDFYRBANCNFSM4KPR2LNA .

tzlaine commented 4 years ago
struct eval 
{
    auto operator() (double a, double b) {
        auto lhs_w = boost::yap::make_terminal<tarray_expr>(std::move(a));
        auto rhs_w = boost::yap::make_terminal<tarray_expr>(std::move(b));

        return lhs_w + rhs_w;
    }
};

This returns a dangling reference to both lhs_w and rhs_w. The documentation explains why. If you std::move them both, as in return std::move(lhs_w) + std::move(rhs_w);, your code will work as intended.