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