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 // UNSUPPORTED: libcpp-no-concepts 11 12 // constexpr decltype(auto) operator[](iter_difference_t<I> n) const 13 // requires random_access_iterator<I>; 14 15 #include <iterator> 16 17 #include "test_macros.h" 18 #include "test_iterators.h" 19 20 template<class Iter> 21 concept SubscriptEnabled = requires(Iter& iter) { 22 iter[1]; 23 }; 24 25 constexpr bool test() { 26 int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; 27 28 { 29 std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8); 30 for (int i = 1; i < 9; ++i) 31 assert(iter[i - 1] == i); 32 } 33 { 34 std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8); 35 for (int i = 1; i < 9; ++i) 36 assert(iter[i - 1] == i); 37 } 38 39 { 40 const std::counted_iterator iter(random_access_iterator<int*>{buffer}, 8); 41 assert(iter[0] == 1); 42 } 43 { 44 const std::counted_iterator iter(contiguous_iterator<int*>{buffer}, 8); 45 assert(iter[7] == 8); 46 } 47 48 { 49 static_assert( SubscriptEnabled<std::counted_iterator<random_access_iterator<int*>>>); 50 static_assert(!SubscriptEnabled<std::counted_iterator<bidirectional_iterator<int*>>>); 51 } 52 53 return true; 54 } 55 56 int main(int, char**) { 57 test(); 58 static_assert(test()); 59 60 return 0; 61 } 62