From e650e5e1abcb615869bf9cfdb2d277ce70b9ef20 Mon Sep 17 00:00:00 2001 From: Alfredo Correa Date: Tue, 11 Feb 2025 23:49:22 -0800 Subject: [PATCH] allow use of 1d array iterator as pointer argument --- include/boost/multi/array_ref.hpp | 13 ++++++++----- test/array_ref.cpp | 23 ++++++++++++++++++++++- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/include/boost/multi/array_ref.hpp b/include/boost/multi/array_ref.hpp index 2101239df..446bf6f05 100644 --- a/include/boost/multi/array_ref.hpp +++ b/include/boost/multi/array_ref.hpp @@ -497,6 +497,9 @@ struct array_iterator // NOLINT(fuchsia-multiple-inheritance) for facades >; using const_reference = const_subarray; // TODO(correaa) should be const_subarray (base of subarray) + template + using rebind = array_iterator, D, typename std::pointer_traits::template rebind, IsConst, IsMove, Stride>; + using iterator_category = std::random_access_iterator_tag; constexpr static dimensionality_type rank_v = D; @@ -2376,6 +2379,9 @@ struct array_iterator // NOLINT(fuchs using iterator_concept = typename stride_traits::category; using element_type = typename std::pointer_traits::element_type; // workaround for clang 15 and libc++ in c++20 mode + template + using rebind = array_iterator, 1, typename std::pointer_traits::template rebind, IsConst, IsMove, Stride>; + static constexpr dimensionality_type dimensionality = 1; #if __cplusplus >= 202002L @@ -3625,11 +3631,8 @@ class array_ref : public subarray #pragma clang diagnostic pop #endif -template -using array_cref = array_ref< - std::decay_t, D, - typename std::pointer_traits::template rebind ->; +template::template rebind > +using array_cref = array_ref, D, Ptr>; template using array_mref = array_ref< diff --git a/test/array_ref.cpp b/test/array_ref.cpp index 0b1de8acb..e9da7d945 100644 --- a/test/array_ref.cpp +++ b/test/array_ref.cpp @@ -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 @@ -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 const arr = {1, 2, 3, 4, 5, 6}; + multi::array_ref::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 const arr = { {1, 2}, {3, 4}, {5, 6}, {7, 8}}; + // multi::array_ref::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