xref: /llvm-project/libcxx/test/std/strings/basic.string/string.iterators/cbegin.pass.cpp (revision 6e1dcc9335116f650d68cdbed12bbb34a99b2d9b)
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_iterator cbegin() const; // constexpr since C++20
12 
13 #include <string>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 #include "min_allocator.h"
18 
19 template <class S>
test(const S & s)20 TEST_CONSTEXPR_CXX20 void test(const S& s) {
21   typename S::const_iterator cb = s.cbegin();
22   if (!s.empty()) {
23     assert(*cb == s[0]);
24   }
25   assert(cb == s.begin());
26 }
27 
28 template <class S>
test_string()29 TEST_CONSTEXPR_CXX20 void test_string() {
30   test(S());
31   test(S("123"));
32 }
33 
test()34 TEST_CONSTEXPR_CXX20 bool test() {
35   test_string<std::string>();
36 #if TEST_STD_VER >= 11
37   test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
38 #endif
39 
40   return true;
41 }
42 
main(int,char **)43 int main(int, char**) {
44   test();
45 #if TEST_STD_VER > 17
46   static_assert(test());
47 #endif
48 
49   return 0;
50 }
51