Skip to content

Commit

Permalink
1-tuple: fix copy assignment of tuple containing refs (#1812)
Browse files Browse the repository at this point in the history
follow up on #1811
  • Loading branch information
havogt authored Nov 13, 2024
1 parent 656d47b commit 5570fa5
Show file tree
Hide file tree
Showing 2 changed files with 18 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 @@ -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) {}

Expand All @@ -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 <class Arg, std::enable_if_t<std::is_assignable_v<T &, Arg const &>, int> = 0>
constexpr GT_FUNCTION tuple &operator=(tuple<Arg> const &src) noexcept {
m_value = src.m_value;
Expand Down
9 changes: 9 additions & 0 deletions tests/unit_tests/common/test_tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<tuple<int &>> src = {{src_int}};
tuple<tuple<int &>> testee = {{dst_int}};
testee = src;
EXPECT_EQ(1, get<0>(get<0>(testee)));
}

TEST(tuple, move_assign) {
tuple<move_only, move_only> testee;
auto &res = testee = tuple<move_only, move_only>{move_only{47}, move_only{2}};
Expand Down

0 comments on commit 5570fa5

Please sign in to comment.