From 9b8715e70612868b597b1d28a738be0ed8dcc6e9 Mon Sep 17 00:00:00 2001 From: Hannes Vogt Date: Mon, 11 Nov 2024 09:35:30 +0100 Subject: [PATCH] fix copy assignment of tuple containing refs (#1811) --- include/gridtools/common/tuple.hpp | 11 +++++++++-- tests/unit_tests/common/test_hymap.cpp | 12 ++++++++++++ tests/unit_tests/common/test_tuple.cpp | 12 ++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/include/gridtools/common/tuple.hpp b/include/gridtools/common/tuple.hpp index 8c2346c0d..5e772ecf4 100644 --- a/include/gridtools/common/tuple.hpp +++ b/include/gridtools/common/tuple.hpp @@ -28,13 +28,20 @@ namespace gridtools { tuple_leaf(tuple_leaf const &) = default; tuple_leaf(tuple_leaf &&) = default; - tuple_leaf &operator=(tuple_leaf const &) = default; - tuple_leaf &operator=(tuple_leaf &&) = default; constexpr GT_FUNCTION tuple_leaf() noexcept : m_value() {} template , int> = 0> constexpr GT_FUNCTION tuple_leaf(Arg &&arg) noexcept : m_value(std::forward(arg)) {} + + constexpr GT_FUNCTION tuple_leaf &operator=(tuple_leaf const &other) noexcept { + m_value = other.m_value; + return *this; + } + constexpr GT_FUNCTION tuple_leaf &operator=(tuple_leaf &&other) noexcept { + m_value = std::move(other.m_value); + return *this; + } }; template diff --git a/tests/unit_tests/common/test_hymap.cpp b/tests/unit_tests/common/test_hymap.cpp index 91e4fc470..af73cb23d 100644 --- a/tests/unit_tests/common/test_hymap.cpp +++ b/tests/unit_tests/common/test_hymap.cpp @@ -222,5 +222,17 @@ namespace gridtools { EXPECT_EQ(88, at_key(testee)); } + TEST(assignment, references) { + double testee_a = 0.; + double testee_b = 0.; + hymap::keys::values testee{testee_a, testee_b}; + double src_a = 3.5; + double src_b = 88; + hymap::keys::values src{src_a, src_b}; + testee = src; + EXPECT_EQ(3.5, at_key(testee)); + EXPECT_EQ(88, at_key(testee)); + } + } // namespace } // namespace gridtools diff --git a/tests/unit_tests/common/test_tuple.cpp b/tests/unit_tests/common/test_tuple.cpp index 94c356863..8f699ca72 100644 --- a/tests/unit_tests/common/test_tuple.cpp +++ b/tests/unit_tests/common/test_tuple.cpp @@ -205,6 +205,18 @@ namespace gridtools { EXPECT_EQ(1.5, get<1>(testee)); } + TEST(tuple, copy_assign_nested_ref) { + int src_int = 1; + double src_double = 1.5; + int dst_int = 0; + double dst_double = 0.; + tuple> src = {{src_int, src_double}}; + tuple> testee = {{dst_int, dst_double}}; + testee = src; + EXPECT_EQ(1, get<0>(get<0>(testee))); + EXPECT_EQ(1.5, get<1>(get<0>(testee))); + } + TEST(one_tuple, copy_assign) { tuple src = {1}; tuple testee;