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// UNSUPPORTED: c++03, c++11, c++14, c++17
10
11// projected
12
13#include <iterator>
14
15#include <concepts>
16#include <functional>
17
18#include "test_iterators.h"
19
20using IntPtr = std::projected<int const*, std::identity>;
21static_assert(std::same_as<IntPtr::value_type, int>);
22static_assert(std::same_as<decltype(*std::declval<IntPtr>()), int const&>);
23static_assert(std::same_as<std::iter_difference_t<IntPtr>, std::ptrdiff_t>);
24
25struct S { };
26
27using Cpp17InputIterator = std::projected<cpp17_input_iterator<S*>, int S::*>;
28static_assert(std::same_as<Cpp17InputIterator::value_type, int>);
29static_assert(std::same_as<decltype(*std::declval<Cpp17InputIterator>()), int&>);
30static_assert(std::same_as<std::iter_difference_t<Cpp17InputIterator>, std::ptrdiff_t>);
31
32using Cpp20InputIterator = std::projected<cpp20_input_iterator<S*>, int S::*>;
33static_assert(std::same_as<Cpp20InputIterator::value_type, int>);
34static_assert(std::same_as<decltype(*std::declval<Cpp20InputIterator>()), int&>);
35static_assert(std::same_as<std::iter_difference_t<Cpp20InputIterator>, std::ptrdiff_t>);
36
37using ForwardIterator = std::projected<forward_iterator<S*>, int (S::*)()>;
38static_assert(std::same_as<ForwardIterator::value_type, int>);
39static_assert(std::same_as<decltype(*std::declval<ForwardIterator>()), int>);
40static_assert(std::same_as<std::iter_difference_t<ForwardIterator>, std::ptrdiff_t>);
41
42using BidirectionalIterator = std::projected<bidirectional_iterator<S*>, S* (S::*)() const>;
43static_assert(std::same_as<BidirectionalIterator::value_type, S*>);
44static_assert(std::same_as<decltype(*std::declval<BidirectionalIterator>()), S*>);
45static_assert(std::same_as<std::iter_difference_t<BidirectionalIterator>, std::ptrdiff_t>);
46
47using RandomAccessIterator = std::projected<random_access_iterator<S*>, S && (S::*)()>;
48static_assert(std::same_as<RandomAccessIterator::value_type, S>);
49static_assert(std::same_as<decltype(*std::declval<RandomAccessIterator>()), S&&>);
50static_assert(std::same_as<std::iter_difference_t<RandomAccessIterator>, std::ptrdiff_t>);
51
52using ContiguousIterator = std::projected<contiguous_iterator<S*>, S& (S::*)() const>;
53static_assert(std::same_as<ContiguousIterator::value_type, S>);
54static_assert(std::same_as<decltype(*std::declval<ContiguousIterator>()), S&>);
55static_assert(std::same_as<std::iter_difference_t<ContiguousIterator>, std::ptrdiff_t>);
56
57template <class I, class F>
58constexpr bool projectable = requires {
59 typename std::projected<I, F>;
60};
61
62static_assert(!projectable<int, void (*)(int)>); // int isn't indirectly_readable
63static_assert(!projectable<S, void (*)(int)>); // S isn't weakly_incrementable
64static_assert(!projectable<int*, void(int)>); // void(int) doesn't satisfy indirectly_regular_unary_invcable
65

source code of libcxx/test/std/iterators/iterator.requirements/indirectcallable/projected/projected.compile.pass.cpp