1// Copyright (C) 2019 T. Zachary Laine
2//
3// Distributed under the Boost Software License, Version 1.0. (See
4// accompanying file LICENSE_1_0.txt or copy at
5// http://www.boost.org/LICENSE_1_0.txt)
6//[ interoperability
7#include <boost/stl_interfaces/iterator_interface.hpp>
8
9#include <algorithm>
10#include <array>
11#include <numeric>
12
13#include <cassert>
14
15
16// This is a random access iterator templated on a value type. The ValueType
17// template parameter allows us easily to define const and non-const iterators
18// from the same template.
19template<typename ValueType>
20struct random_access_iterator : boost::stl_interfaces::iterator_interface<
21#if !BOOST_STL_INTERFACES_USE_DEDUCED_THIS
22 random_access_iterator<ValueType>,
23#endif
24 std::random_access_iterator_tag,
25 ValueType>
26{
27 static_assert(std::is_object<ValueType>::value, "");
28
29 // Default constructor.
30 constexpr random_access_iterator() noexcept {}
31
32 // Construction from an underlying pointer.
33 constexpr random_access_iterator(ValueType * it) noexcept : it_(it) {}
34
35 // Implicit conversion from an existing random_access_iterator with a
36 // possibly different value type. The enable_if logic here just enforces
37 // that this constructor only participates in overload resolution when the
38 // expression it_ = other.it_ is well-formed.
39 template<
40 typename ValueType2,
41 typename E = std::enable_if_t<
42 std::is_convertible<ValueType2 *, ValueType *>::value>>
43 constexpr random_access_iterator(
44 random_access_iterator<ValueType2> other) noexcept :
45 it_(other.it_)
46 {}
47
48 constexpr ValueType & operator*() const noexcept { return *it_; }
49 constexpr random_access_iterator & operator+=(std::ptrdiff_t i) noexcept
50 {
51 it_ += i;
52 return *this;
53 }
54 constexpr auto operator-(random_access_iterator other) const noexcept
55 {
56 return it_ - other.it_;
57 }
58
59private:
60 ValueType * it_;
61
62 // This friendship is necessary to enable the implicit conversion
63 // constructor above to work.
64 template<typename ValueType2>
65 friend struct random_access_iterator;
66};
67
68using iterator = random_access_iterator<int>;
69using const_iterator = random_access_iterator<int const>;
70
71int main()
72{
73 std::array<int, 10> ints = {._M_elems: {0, 2, 1, 3, 4, 5, 7, 6, 8, 9}};
74
75 // Create and use two mutable iterators.
76 iterator first(ints.data());
77 iterator last(ints.data() + ints.size());
78 std::sort(first: first, last: last);
79 assert(ints == (std::array<int, 10>{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}));
80
81 // Create and use two constant iterators, one from an existing mutable
82 // iterator.
83 std::array<int, 10> int_sums;
84 const_iterator cfirst(ints.data());
85 const_iterator clast = last;
86 std::partial_sum(first: cfirst, last: clast, result: int_sums.begin());
87 assert(int_sums == (std::array<int, 10>{{0, 1, 3, 6, 10, 15, 21, 28, 36, 45}}));
88}
89//]
90

source code of boost/libs/stl_interfaces/example/interoperability.cpp