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// check that std::__unwrap_iter() returns the correct type
10
11#include <algorithm>
12#include <cassert>
13#include <string>
14#include <type_traits>
15
16#include "test_iterators.h"
17#include "test_macros.h"
18
19template <class Iter>
20using UnwrapT = decltype(std::__unwrap_iter(std::declval<Iter>()));
21
22template <class Iter>
23using rev_iter = std::reverse_iterator<Iter>;
24
25template <class Iter>
26using rev_rev_iter = rev_iter<rev_iter<Iter> >;
27
28static_assert(std::is_same<UnwrapT<int*>, int*>::value, "");
29static_assert(std::is_same<UnwrapT<std::__wrap_iter<int*> >, int*>::value, "");
30static_assert(std::is_same<UnwrapT<rev_iter<int*> >, std::reverse_iterator<int*> >::value, "");
31static_assert(std::is_same<UnwrapT<rev_rev_iter<int*> >, int*>::value, "");
32static_assert(std::is_same<UnwrapT<rev_rev_iter<std::__wrap_iter<int*> > >, int*>::value, "");
33static_assert(std::is_same<UnwrapT<rev_rev_iter<rev_iter<std::__wrap_iter<int*> > > >, rev_iter<std::__wrap_iter<int*> > >::value, "");
34
35static_assert(std::is_same<UnwrapT<random_access_iterator<int*> >, random_access_iterator<int*> >::value, "");
36static_assert(std::is_same<UnwrapT<rev_iter<random_access_iterator<int*> > >, rev_iter<random_access_iterator<int*> > >::value, "");
37static_assert(std::is_same<UnwrapT<rev_rev_iter<random_access_iterator<int*> > >, random_access_iterator<int*> >::value, "");
38static_assert(std::is_same<UnwrapT<rev_rev_iter<rev_iter<random_access_iterator<int*> > > >, rev_iter<random_access_iterator<int*> > >::value, "");
39
40TEST_CONSTEXPR_CXX20 bool test() {
41 std::string str = "Banane";
42 using Iter = std::string::iterator;
43
44 assert(std::__unwrap_iter(str.begin()) == str.data());
45 assert(std::__unwrap_iter(str.end()) == str.data() + str.size());
46 assert(std::__unwrap_iter(rev_rev_iter<Iter>(rev_iter<Iter>(str.begin()))) == str.data());
47 assert(std::__unwrap_iter(rev_rev_iter<Iter>(rev_iter<Iter>(str.end()))) == str.data() + str.size());
48
49 return true;
50}
51
52int main(int, char**) {
53 test();
54#if TEST_STD_VER > 17
55 static_assert(test());
56#endif
57
58 return 0;
59}
60

source code of libcxx/test/libcxx/iterators/unwrap_iter.pass.cpp