From e825b236ee0809c17cdb6fa6f651854376044a8a Mon Sep 17 00:00:00 2001 From: Heiko Lewin Date: Tue, 25 Feb 2025 21:01:39 +0100 Subject: [PATCH] Parser_default: Move temporary variable instead of copying (#362) --- include/rfl/parsing/Parser_default.hpp | 2 +- tests/json/test_movable.cpp | 28 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/json/test_movable.cpp diff --git a/include/rfl/parsing/Parser_default.hpp b/include/rfl/parsing/Parser_default.hpp index 0d7c8502..5bdf21cc 100644 --- a/include/rfl/parsing/Parser_default.hpp +++ b/include/rfl/parsing/Parser_default.hpp @@ -69,7 +69,7 @@ struct Parser { using ReflectionType = std::remove_cvref_t; const auto wrap_in_t = [](auto _named_tuple) -> Result { try { - return T{_named_tuple}; + return T{std::move(_named_tuple)}; } catch (std::exception& e) { return error(e.what()); } diff --git a/tests/json/test_movable.cpp b/tests/json/test_movable.cpp new file mode 100644 index 00000000..6bc420e2 --- /dev/null +++ b/tests/json/test_movable.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + + +struct MoveableType { + int x; + + + MoveableType(MoveableType&&) = default; + MoveableType(const MoveableType&) = delete; + + using ReflectionType = int; + MoveableType(int&& x) : x(x) {} + int reflection() const { return x; } +}; + +TEST(json, test_moveable) { + MoveableType moveable = {2}; + + write_and_read( + moveable, R"(2)"); +} \ No newline at end of file