Skip to content

Commit

Permalink
allow use of 1d array iterator as pointer argument
Browse files Browse the repository at this point in the history
  • Loading branch information
alfC committed Feb 12, 2025
1 parent 830e94a commit e650e5e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
13 changes: 8 additions & 5 deletions include/boost/multi/array_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@ struct array_iterator // NOLINT(fuchsia-multiple-inheritance) for facades
>;
using const_reference = const_subarray<element, D - 1, element_ptr>; // TODO(correaa) should be const_subarray (base of subarray)

template<class Element2>
using rebind = array_iterator<std::decay_t<Element2>, D, typename std::pointer_traits<ElementPtr>::template rebind<Element2>, IsConst, IsMove, Stride>;

using iterator_category = std::random_access_iterator_tag;

constexpr static dimensionality_type rank_v = D;
Expand Down Expand Up @@ -2376,6 +2379,9 @@ struct array_iterator<Element, 1, Ptr, IsConst, IsMove, Stride> // NOLINT(fuchs
using iterator_concept = typename stride_traits<Stride>::category;
using element_type = typename std::pointer_traits<Ptr>::element_type; // workaround for clang 15 and libc++ in c++20 mode

template<class Element2>
using rebind = array_iterator<std::decay_t<Element2>, 1, typename std::pointer_traits<Ptr>::template rebind<Element2>, IsConst, IsMove, Stride>;

static constexpr dimensionality_type dimensionality = 1;

#if __cplusplus >= 202002L
Expand Down Expand Up @@ -3625,11 +3631,8 @@ class array_ref : public subarray<T, D, ElementPtr, Layout>
#pragma clang diagnostic pop
#endif

template<class T, dimensionality_type D, class Ptr = T*>
using array_cref = array_ref<
std::decay_t<T>, D,
typename std::pointer_traits<Ptr>::template rebind<T const>
>;
template<class T, dimensionality_type D, class Ptr = typename std::pointer_traits<T*>::template rebind<T const> >
using array_cref = array_ref<std::decay_t<T>, D, Ptr>;

template<class T, dimensionality_type D, class Ptr = T*>
using array_mref = array_ref<
Expand Down
23 changes: 22 additions & 1 deletion test/array_ref.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2024 Alfredo A. Correa
// Copyright 2019-2025 Alfredo A. Correa
// Copyright 2024 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
Expand Down Expand Up @@ -243,6 +243,27 @@ BOOST_AUTO_TEST_CASE(array_ref_test_no_ub) {
BOOST_TEST( std::accumulate(diag.begin(), diag.end(), 0) == 0 + 60 + 120 + 180 );
}

/*use iterator as pointer*/
{
multi::array<int, 1> const arr = {1, 2, 3, 4, 5, 6};
multi::array_ref<int, 2, multi::array<int, 1>::const_iterator> const arr2({2, 3}, arr.begin());
BOOST_TEST(( arr2 == multi::array{
{1, 2, 3},
{4, 5, 6}
}));
}

/*use iterator as pointer*/
{
// multi::array<int, 2> const arr = { {1, 2}, {3, 4}, {5, 6}, {7, 8}};
// multi::array_ref<int, 2, multi::array<int, 2>::const_iterator> const arr2({2, 2}, arr.begin());
// BOOST_TEST(( arr2 == multi::array{
// {1, 2, 3},
// {4, 5, 6}
// }));
}


BOOST_AUTO_TEST_CASE(array_ref_test_no_ub2) {
#if defined(__clang__)
#pragma clang diagnostic push
Expand Down

0 comments on commit e650e5e

Please sign in to comment.