boostorg / leaf

Lightweight Error Augmentation Framework
Boost Software License 1.0
313 stars 48 forks source link

Providing leaf::result with forwarding constructor and emplace method #70

Closed fedraco-gh closed 9 months ago

fedraco-gh commented 9 months ago

Would it be possible to expose a leaf::result constructor to directly initialize its value from a parameter pack as it is provided by constructor 9 of std::expected?

I think it would be useful when dealing with return types that are both expensive to copy and move. For the same reason an emplace method might come in handy.

zajo commented 9 months ago

You do this by invoking new_error, which does take a parameter pack:

result<int> f()
{
    if( .... )
        return leaf::new_error(e1, e2, ...);
}

Note that in LEAF result<> types never hold any error objects, so moving an object of a result<> type is efficient even when e1, e2, ... are extremely expensive to move.

fedraco-gh commented 9 months ago

Thank you for your reply. I was referring to the object that is returned if no error arises.

Please refer to this example on Compiler Explorer: https://godbolt.org/z/7nefnxnrv.

With throwing_foo there are no copies being made thanks to prvalue materialization rules. With noexcept_foo, which returns a leaf::result, one copy takes place.

If it was possible to initialize leaf::result directly with the T constructor parameters the copy would not happen.