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 // XFAIL: LIBCXX-AIX-FIXME
10 
11 // <string>
12 
13 // const_reference operator[](size_type pos) const; // constexpr since C++20
14 //       reference operator[](size_type pos); // constexpr since C++20
15 
16 #include <string>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 #include "min_allocator.h"
21 
22 TEST_CONSTEXPR_CXX20 bool test() {
23   {
24     typedef std::string S;
25     S s("0123456789");
26     const S& cs = s;
27     ASSERT_SAME_TYPE(decltype( s[0]), typename S::reference);
28     ASSERT_SAME_TYPE(decltype(cs[0]), typename S::const_reference);
29     LIBCPP_ASSERT_NOEXCEPT(    s[0]);
30     LIBCPP_ASSERT_NOEXCEPT(   cs[0]);
31     for (S::size_type i = 0; i < cs.size(); ++i)
32     {
33         assert(s[i] == static_cast<char>('0' + i));
34         assert(cs[i] == s[i]);
35     }
36     assert(cs[cs.size()] == '\0');
37     const S s2 = S();
38     assert(s2[0] == '\0');
39   }
40 #if TEST_STD_VER >= 11
41   {
42     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
43     S s("0123456789");
44     const S& cs = s;
45     ASSERT_SAME_TYPE(decltype( s[0]), typename S::reference);
46     ASSERT_SAME_TYPE(decltype(cs[0]), typename S::const_reference);
47     LIBCPP_ASSERT_NOEXCEPT(    s[0]);
48     LIBCPP_ASSERT_NOEXCEPT(   cs[0]);
49     for (S::size_type i = 0; i < cs.size(); ++i)
50     {
51         assert(s[i] == static_cast<char>('0' + i));
52         assert(cs[i] == s[i]);
53     }
54     assert(cs[cs.size()] == '\0');
55     const S s2 = S();
56     assert(s2[0] == '\0');
57   }
58 #endif
59 
60   return true;
61 }
62 
63 int main(int, char**)
64 {
65   test();
66 #if TEST_STD_VER > 17
67   static_assert(test());
68 #endif
69 
70   return 0;
71 }
72