From dad38d2058abddb443690589cc0ec58c9fd440b6 Mon Sep 17 00:00:00 2001 From: Ishan Karmakar Date: Fri, 9 Aug 2024 08:13:01 +0100 Subject: [PATCH 1/3] Move all frg::array get overloads to frg namespace --- include/frg/array.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/frg/array.hpp b/include/frg/array.hpp index 0184edf..e5d9bbb 100644 --- a/include/frg/array.hpp +++ b/include/frg/array.hpp @@ -124,10 +124,6 @@ constexpr auto array_concat(const Ts &...arrays) { return res; } -} // namespace frg - -namespace std { - template constexpr T &get(frg::array &a) noexcept { static_assert(I < N, "array index is not within bounds"); @@ -152,6 +148,10 @@ constexpr const T &&get(const frg::array &&a) noexcept { return std::move(a[I]); }; +} // namespace frg + +namespace std { + template struct tuple_size> : integral_constant { }; From fdb7cb0aae10000e55c03645c415ad7921fef07f Mon Sep 17 00:00:00 2001 From: Ishan Karmakar Date: Mon, 12 Aug 2024 19:36:06 -0500 Subject: [PATCH 2/3] Added get functions back to std for backwards compatibility --- include/frg/array.hpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/frg/array.hpp b/include/frg/array.hpp index e5d9bbb..64c8c45 100644 --- a/include/frg/array.hpp +++ b/include/frg/array.hpp @@ -152,6 +152,30 @@ constexpr const T &&get(const frg::array &&a) noexcept { namespace std { +template +constexpr T &get(frg::array &a) noexcept { + static_assert(I < N, "array index is not within bounds"); + return a[I]; +}; + +template +constexpr T &&get(frg::array &&a) noexcept { + static_assert(I < N, "array index is not within bounds"); + return std::move(a[I]); +}; + +template +constexpr const T &get(const frg::array &a) noexcept { + static_assert(I < N, "array index is not within bounds"); + return a[I]; +}; + +template +constexpr const T &&get(const frg::array &&a) noexcept { + static_assert(I < N, "array index is not within bounds"); + return std::move(a[I]); +}; + template struct tuple_size> : integral_constant { }; From 82ce0ca7d0c7cb8b9b206e04231ba738ef949af4 Mon Sep 17 00:00:00 2001 From: Ishan Karmakar Date: Mon, 12 Aug 2024 20:36:26 -0500 Subject: [PATCH 3/3] Added tests --- include/frg/array.hpp | 24 ------------------------ tests/tests.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/include/frg/array.hpp b/include/frg/array.hpp index 64c8c45..e5d9bbb 100644 --- a/include/frg/array.hpp +++ b/include/frg/array.hpp @@ -152,30 +152,6 @@ constexpr const T &&get(const frg::array &&a) noexcept { namespace std { -template -constexpr T &get(frg::array &a) noexcept { - static_assert(I < N, "array index is not within bounds"); - return a[I]; -}; - -template -constexpr T &&get(frg::array &&a) noexcept { - static_assert(I < N, "array index is not within bounds"); - return std::move(a[I]); -}; - -template -constexpr const T &get(const frg::array &a) noexcept { - static_assert(I < N, "array index is not within bounds"); - return a[I]; -}; - -template -constexpr const T &&get(const frg::array &&a) noexcept { - static_assert(I < N, "array index is not within bounds"); - return std::move(a[I]); -}; - template struct tuple_size> : integral_constant { }; diff --git a/tests/tests.cpp b/tests/tests.cpp index e247891..8260bd5 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -149,6 +149,32 @@ TEST(tuples, reference_test) { EXPECT_EQ(&z, &t2.get<2>()); } +#include + +TEST(array, basic_test) { + constexpr int N = 4; + frg::array arr{0, 1, 2, 3}; + for (int i = 0; i < N; i++) + EXPECT_EQ(arr[i], i); + + const auto [a, b, c, d] = arr; + EXPECT_EQ(a, 0); + EXPECT_EQ(b, 1); + EXPECT_EQ(c, 2); + EXPECT_EQ(d, 3); + + arr[0] = 1; + EXPECT_NE(a, arr[0]); // Make sure a doesn't change when array changes + + const auto& [e, f, g, h] = arr; + EXPECT_EQ(e, 1); + arr[0] = 2; + EXPECT_EQ(e, 2); // Make sure e does change when array changes + + static_assert(std::tuple_size_v == N, "tuple_size produces wrong result"); + static_assert(std::is_same_v, int>, "tuple_element produces wrong result"); +} + #include #include #include // std::string