pw_function: Fix for move-only types
- Add std::forward to support move-only function arguments.
- Mark default constructors constexpr.
Change-Id: I505ae0a40919b61ad7be927d19273ab22354f568
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/79501
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Reviewed-by: Alexei Frolov <frolv@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
diff --git a/pw_function/function_test.cc b/pw_function/function_test.cc
index 0c2b89a..541a4c7 100644
--- a/pw_function/function_test.cc
+++ b/pw_function/function_test.cc
@@ -247,6 +247,28 @@
#endif // __clang_analyzer__
}
+TEST(Function, MoveOnlyType) {
+ class MoveOnlyType {
+ public:
+ MoveOnlyType() = default;
+
+ MoveOnlyType(const MoveOnlyType& other) = delete;
+ MoveOnlyType& operator=(const MoveOnlyType& other) = delete;
+
+ MoveOnlyType(MoveOnlyType&&) = default;
+ MoveOnlyType& operator=(MoveOnlyType&&) = default;
+
+ bool ItsWorking() const { return true; }
+ };
+
+ pw::Function<bool(MoveOnlyType)> function = [](MoveOnlyType value) {
+ return value.ItsWorking();
+ };
+
+ MoveOnlyType move_only;
+ EXPECT_TRUE(function(std::move(move_only)));
+}
+
} // namespace
} // namespace pw