Skip to content

Commit

Permalink
fix copy assignment of tuple containing refs (#1811)
Browse files Browse the repository at this point in the history
  • Loading branch information
havogt committed Nov 11, 2024
1 parent 891533a commit 9b8715e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
11 changes: 9 additions & 2 deletions include/gridtools/common/tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class Arg, std::enable_if_t<std::is_constructible_v<T, Arg &&>, int> = 0>
constexpr GT_FUNCTION tuple_leaf(Arg &&arg) noexcept : m_value(std::forward<Arg>(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 <size_t I, class T>
Expand Down
12 changes: 12 additions & 0 deletions tests/unit_tests/common/test_hymap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,17 @@ namespace gridtools {
EXPECT_EQ(88, at_key<b>(testee));
}

TEST(assignment, references) {
double testee_a = 0.;
double testee_b = 0.;
hymap::keys<a, b>::values<double &, double &> testee{testee_a, testee_b};
double src_a = 3.5;
double src_b = 88;
hymap::keys<a, b>::values<double &, double &> src{src_a, src_b};
testee = src;
EXPECT_EQ(3.5, at_key<a>(testee));
EXPECT_EQ(88, at_key<b>(testee));
}

} // namespace
} // namespace gridtools
12 changes: 12 additions & 0 deletions tests/unit_tests/common/test_tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<tuple<int &, double &>> src = {{src_int, src_double}};
tuple<tuple<int &, double &>> 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<int> src = {1};
tuple<int> testee;
Expand Down

0 comments on commit 9b8715e

Please sign in to comment.