Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for structured bindings from const camp::tuple #140

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/camp/tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ namespace std {

template <size_t i, typename ... T>
struct tuple_element<i, camp::tuple<T...>> {
using type = decltype(camp::get<i>(camp::tuple<T...>{}));
using type = ::camp::tuple_element_t<i, camp::tuple<T...>>;
};

/// This allows structured bindings to be used with camp::tagged_tuple
Expand All @@ -718,7 +718,7 @@ namespace std {

template <size_t i, typename TagList, typename... Elements>
struct tuple_element<i, camp::tagged_tuple<TagList, Elements...>> {
using type = decltype(camp::get<i>(camp::tagged_tuple<TagList, Elements...>{}));
using type = ::camp::tuple_element_t<i, camp::tagged_tuple<TagList, Elements...>>;
};
} // namespace std
#endif
Expand Down
37 changes: 37 additions & 0 deletions test/tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,43 @@ TEST(CampTuple, StructuredBindings)
{
auto t = camp::make_tuple(3, 9.9);
auto [a, b] = t;
static_assert(std::is_same< decltype(a), int >::value );
static_assert(std::is_same< decltype(b), double >::value );
ASSERT_EQ(a, 3);
ASSERT_NEAR(b, 9.9, 1e-15);
}

TEST(CampTuple, StructuredBindingsConst)
{
auto t = camp::make_tuple(3, 9.9);
const auto [a, b] = t;
static_assert(std::is_same< decltype(a), const int >::value );
static_assert(std::is_same< decltype(b), const double >::value );
ASSERT_EQ(a, 3);
ASSERT_NEAR(b, 9.9, 1e-15);
}

TEST(CampTuple, StructuredBindingsRef)
{
auto t = camp::make_tuple(3, 9.9);
auto & [a, b] = t;
static_assert(std::is_same< decltype(a), int >::value ); // Not an int &
static_assert(std::is_same< decltype(b), double >::value ); // Not a double &
ASSERT_EQ(a, 3);
ASSERT_NEAR(b, 9.9, 1e-15);

a = 4; // Well...kind of an int &
b = 10.1; // also kind of a double &
ASSERT_EQ(camp::get<0>(t), 4);
ASSERT_NEAR(camp::get<1>(t), 10.1, 1e-15);
}

TEST(CampTuple, StructuredBindingsRefConst)
{
auto t = camp::make_tuple(3, 9.9);
const auto & [a, b] = t;
static_assert(std::is_same< decltype(a), const int >::value ); // Not an const int &
static_assert(std::is_same< decltype(b), const double >::value ); // Not a const double &
ASSERT_EQ(a, 3);
ASSERT_NEAR(b, 9.9, 1e-15);
}
Expand Down
Loading