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