Skip to content

Commit 2d028f7

Browse files
committed
Adding support for structured bindings to chplx::Tuple
Signed-off-by: Hartmut Kaiser <[email protected]>
1 parent 4fe1562 commit 2d028f7

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

library/include/chplx/tuple.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,42 @@ template <typename... Ts> struct Tuple : std::tuple<Ts...> {
9090
};
9191

9292
template <typename... Ts> Tuple(Ts &&...) -> Tuple<Ts...>;
93+
} // namespace chplx
94+
95+
//-----------------------------------------------------------------------------
96+
// Support structured bindings
97+
namespace std {
98+
99+
template <typename... Ts>
100+
struct tuple_size<chplx::Tuple<Ts...>> : tuple_size<tuple<Ts...>> {};
101+
102+
template <size_t I, typename... Ts>
103+
struct tuple_element<I, chplx::Tuple<Ts...>> : tuple_element<I, tuple<Ts...>> {
104+
};
105+
106+
template <size_t I, typename... Ts>
107+
constexpr auto &get(chplx::Tuple<Ts...> &t) noexcept {
108+
return get<I>(t.base());
109+
}
110+
111+
template <size_t I, typename... Ts>
112+
constexpr auto const &get(chplx::Tuple<Ts...> const &t) noexcept {
113+
return get<I>(t.base());
114+
}
115+
116+
template <size_t I, typename... Ts>
117+
constexpr auto &&get(chplx::Tuple<Ts...> &&t) noexcept {
118+
return get<I>(std::move(t.base()));
119+
}
120+
121+
template <size_t I, typename... Ts>
122+
constexpr auto const &&get(chplx::Tuple<Ts...> const &&t) noexcept {
123+
return get<I>(std::move(t.base()));
124+
}
125+
126+
} // namespace std
127+
128+
namespace chplx {
93129

94130
//-----------------------------------------------------------------------------
95131
// Returns true if t is a tuple; otherwise false.

library/test/unit/tuple.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,19 @@ void testNonHomogenous() {
156156
}
157157
}
158158

159+
void testStructuredBindings() {
160+
chplx::Tuple t(2, 4L, std::string("33"));
161+
auto [a, b, c] = t;
162+
HPX_TEST_EQ(a, 2);
163+
HPX_TEST_EQ(b, 4L);
164+
HPX_TEST_EQ(c, std::string("33"));
165+
}
166+
159167
int main() {
160168

161169
testHomogenous();
162170
testNonHomogenous();
171+
testStructuredBindings();
163172

164173
return hpx::util::report_errors();
165174
}

0 commit comments

Comments
 (0)