difference_type.pass.cpp revision f333beee2ce44299b1ce219371bdef60290ac175
1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <iterator>
11
12// move_iterator
13
14// requires RandomAccessIterator<Iter>
15//   unspecified operator[](difference_type n) const;
16//
17//  constexpr in C++17
18
19#include <iterator>
20#include <cassert>
21#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
22#include <memory>
23#endif
24
25#include "test_macros.h"
26#include "test_iterators.h"
27
28template <class It>
29void
30test(It i, typename std::iterator_traits<It>::difference_type n,
31     typename std::iterator_traits<It>::value_type x)
32{
33    typedef typename std::iterator_traits<It>::value_type value_type;
34    const std::move_iterator<It> r(i);
35    value_type rr = r[n];
36    assert(rr == x);
37}
38
39#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
40
41struct do_nothing
42{
43    void operator()(void*) const {}
44};
45
46#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
47
48int main()
49{
50    char s[] = "1234567890";
51    test(random_access_iterator<char*>(s+5), 4, '0');
52    test(s+5, 4, '0');
53#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
54    int i[5];
55    typedef std::unique_ptr<int, do_nothing> Ptr;
56    Ptr p[5];
57    for (unsigned j = 0; j < 5; ++j)
58        p[j].reset(i+j);
59    test(p, 3, Ptr(i+3));
60#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
61
62#if TEST_STD_VER > 14
63    {
64    constexpr const char *p = "123456789";
65    typedef std::move_iterator<const char *> MI;
66    constexpr MI it1 = std::make_move_iterator(p);
67    static_assert(it1[0] == '1', "");
68    static_assert(it1[5] == '6', "");
69    }
70#endif
71}
72