1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <iterator>
10
11// template <InputIterator Iter>
12// Iter::difference_type
13// distance(Iter first, Iter last); // constexpr in C++17
14//
15// template <RandomAccessIterator Iter>
16// Iter::difference_type
17// distance(Iter first, Iter last); // constexpr in C++17
18
19#include <iterator>
20#include <cassert>
21#include <type_traits>
22
23#include "test_macros.h"
24#include "test_iterators.h"
25
26template <class It>
27TEST_CONSTEXPR_CXX17
28void check_distance(It first, It last, typename std::iterator_traits<It>::difference_type dist)
29{
30 typedef typename std::iterator_traits<It>::difference_type Difference;
31 static_assert(std::is_same<decltype(std::distance(first, last)), Difference>::value, "");
32 assert(std::distance(first, last) == dist);
33}
34
35TEST_CONSTEXPR_CXX17 bool tests()
36{
37 const char* s = "1234567890";
38 check_distance(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 10);
39 check_distance(forward_iterator<const char*>(s), forward_iterator<const char*>(s+10), 10);
40 check_distance(bidirectional_iterator<const char*>(s), bidirectional_iterator<const char*>(s+10), 10);
41 check_distance(random_access_iterator<const char*>(s), random_access_iterator<const char*>(s+10), 10);
42 check_distance(s, s+10, 10);
43 return true;
44}
45
46int main(int, char**)
47{
48 tests();
49#if TEST_STD_VER >= 17
50 static_assert(tests(), "");
51#endif
52 return 0;
53}
54

source code of libcxx/test/std/iterators/iterator.primitives/iterator.operations/distance.pass.cpp