arrow icon indicating copy to clipboard operation
arrow copied to clipboard

[C++] arrow::Result::status() should have rvalue-reference-qualified overload returning value instead of reference

Open igor-anferov opened this issue 1 year ago • 0 comments

Describe the enhancement requested

In current implementation, arrow::Result::status() always returns internal status_ field by const lvalue reference, regardless of Result value category, which can lead to a lot of bugs. For example, consider the following code:

if (auto&& status = functionReturningArrowResult().status(); status.ok())
  return 0;
return -1;

Here call status.ok() leads to undefined behavior cause status is a dangling const lvalue reference pointing to the object returned by functionReturningArrowResult() and destructed after ;.

If arrow::Result had two overloads of status() method for different ref qualifiers:

template <…>
class Result {
  …
  auto status() const & -> const Status& { return status_; }
  auto status() && -> Status { return std::move(status_); }
  …
};

It would prevent such type of bugs, and would allow for better optimized code (as you could move Status from expiring Result object).

Component(s)

C++

igor-anferov avatar Oct 17 '24 15:10 igor-anferov