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

array: Fix structured bindings #73

Merged
merged 3 commits into from
Aug 13, 2024
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
8 changes: 4 additions & 4 deletions include/frg/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ constexpr auto array_concat(const Ts &...arrays) {
return res;
}

} // namespace frg

namespace std {

template<size_t I, class T, size_t N>
constexpr T &get(frg::array<T, N> &a) noexcept {
static_assert(I < N, "array index is not within bounds");
Expand All @@ -152,6 +148,10 @@ constexpr const T &&get(const frg::array<T, N> &&a) noexcept {
return std::move(a[I]);
};

} // namespace frg

namespace std {

template<class T, size_t N>
struct tuple_size<frg::array<T, N>> :
integral_constant<size_t, N> { };
Expand Down
26 changes: 26 additions & 0 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ TEST(tuples, reference_test) {
EXPECT_EQ(&z, &t2.get<2>());
}

#include <frg/array.hpp>

TEST(array, basic_test) {
constexpr int N = 4;
frg::array<int, N> 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<decltype(arr)> == N, "tuple_size produces wrong result");
static_assert(std::is_same_v<std::tuple_element_t<N, decltype(arr)>, int>, "tuple_element produces wrong result");
}

#include <frg/formatting.hpp>
#include <frg/logging.hpp>
#include <string> // std::string
Expand Down
Loading