You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ap...@apache.org on 2023/01/31 10:33:29 UTC

[arrow] branch master updated: GH-33911: [C++] Add missing std::forward to Result::ValueOrElse (#33912)

This is an automated email from the ASF dual-hosted git repository.

apitrou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 7a3c66ac80 GH-33911: [C++] Add missing std::forward to Result::ValueOrElse (#33912)
7a3c66ac80 is described below

commit 7a3c66ac8063bc2b2fe184cc02b1750565421f01
Author: Twice <tw...@apache.org>
AuthorDate: Tue Jan 31 18:33:21 2023 +0800

    GH-33911: [C++] Add missing std::forward to Result::ValueOrElse (#33912)
    
    It fixes #33911.
    
    ------
    
    From #33911:
    
    > https://github.com/apache/arrow/blob/4f1d255f3dc57457e5c78d98c4b76fc523cf9961/cpp/src/arrow/result.h#L369-L375
    >
    > The parameter `generate_alternative` is passed by univeral forwarding, but `std::forward` is missing to use it.
    >
    > This may result in incorrect overloading selection or compilation errors, such as a class type with the method `operator()() &&`.
    * Closes: #33911
    
    Lead-authored-by: PragmaTwice <tw...@gmail.com>
    Co-authored-by: Twice <tw...@apache.org>
    Signed-off-by: Antoine Pitrou <an...@python.org>
---
 cpp/src/arrow/result.h       |  2 +-
 cpp/src/arrow/result_test.cc | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/cpp/src/arrow/result.h b/cpp/src/arrow/result.h
index 30e632c700..6786d2b3fc 100644
--- a/cpp/src/arrow/result.h
+++ b/cpp/src/arrow/result.h
@@ -371,7 +371,7 @@ class [[nodiscard]] Result : public util::EqualityComparable<Result<T>> {
     if (ok()) {
       return MoveValueUnsafe();
     }
-    return generate_alternative();
+    return std::forward<G>(generate_alternative)();
   }
 
   /// Apply a function to the internally stored value to produce a new result or propagate
diff --git a/cpp/src/arrow/result_test.cc b/cpp/src/arrow/result_test.cc
index cb645bc740..794ef9b5dc 100644
--- a/cpp/src/arrow/result_test.cc
+++ b/cpp/src/arrow/result_test.cc
@@ -28,6 +28,7 @@
 #include "arrow/testing/gtest_compat.h"
 #include "arrow/testing/gtest_util.h"
 #include "arrow/testing/matchers.h"
+#include "arrow/util/functional.h"
 
 namespace arrow {
 
@@ -795,5 +796,14 @@ TEST(ResultTest, MatcherExplanations) {
   }
 }
 
+TEST(ResultTest, ValueOrGeneratedMoveOnlyGenerator) {
+  Result<MoveOnlyDataType> result = Status::Invalid("");
+  internal::FnOnce<MoveOnlyDataType()> alternative_generator = [] {
+    return MoveOnlyDataType{kIntElement};
+  };
+  auto out = std::move(result).ValueOrElse(std::move(alternative_generator));
+  EXPECT_EQ(*out.data, kIntElement);
+}
+
 }  // namespace
 }  // namespace arrow