From ea641614342160ca73979ce90a99934f2a84929d Mon Sep 17 00:00:00 2001 From: Hannes Vogt Date: Fri, 8 Nov 2024 20:45:13 +0100 Subject: [PATCH 1/2] fix copy_assignment of tuple containing refs --- 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; From 0c26727093150fd003f9e17856547763aca50b90 Mon Sep 17 00:00:00 2001 From: Hannes Vogt Date: Mon, 11 Nov 2024 11:43:19 +0100 Subject: [PATCH 2/2] fix one-tuple --- include/gridtools/common/tuple.hpp | 11 +++++++++-- tests/unit_tests/common/test_tuple.cpp | 9 +++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/gridtools/common/tuple.hpp b/include/gridtools/common/tuple.hpp index 5e772ecf4..1fc522522 100644 --- a/include/gridtools/common/tuple.hpp +++ b/include/gridtools/common/tuple.hpp @@ -235,8 +235,6 @@ namespace gridtools { tuple(tuple const &) = default; tuple(tuple &&) = default; - tuple &operator=(tuple const &) = default; - tuple &operator=(tuple &&) = default; constexpr GT_FUNCTION tuple(T const &arg) noexcept : m_value(arg) {} @@ -260,6 +258,15 @@ namespace gridtools { swap(m_value, other.m_value); } + constexpr GT_FUNCTION tuple &operator=(tuple const &other) noexcept { + m_value = other.m_value; + return *this; + } + constexpr GT_FUNCTION tuple &operator=(tuple &&other) noexcept { + m_value = std::move(other.m_value); + return *this; + } + template , int> = 0> constexpr GT_FUNCTION tuple &operator=(tuple const &src) noexcept { m_value = src.m_value; diff --git a/tests/unit_tests/common/test_tuple.cpp b/tests/unit_tests/common/test_tuple.cpp index 8f699ca72..85f44c371 100644 --- a/tests/unit_tests/common/test_tuple.cpp +++ b/tests/unit_tests/common/test_tuple.cpp @@ -226,6 +226,15 @@ namespace gridtools { EXPECT_EQ(1, get<0>(testee)); } + TEST(onetuple, copy_assign_nested_ref) { + int src_int = 1; + int dst_int = 0; + tuple> src = {{src_int}}; + tuple> testee = {{dst_int}}; + testee = src; + EXPECT_EQ(1, get<0>(get<0>(testee))); + } + TEST(tuple, move_assign) { tuple testee; auto &res = testee = tuple{move_only{47}, move_only{2}};