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 charT* c_str() const; // constexpr since C++20
12
13#include <string>
14#include <cassert>
15
16#include "test_macros.h"
17#include "min_allocator.h"
18
19template <class S>
20TEST_CONSTEXPR_CXX20 void test(const S& s) {
21 typedef typename S::traits_type T;
22 const typename S::value_type* str = s.c_str();
23 if (s.size() > 0) {
24 assert(T::compare(str, &s[0], s.size()) == 0);
25 assert(T::eq(str[s.size()], typename S::value_type()));
26 } else
27 assert(T::eq(str[0], typename S::value_type()));
28}
29
30template <class S>
31TEST_CONSTEXPR_CXX20 void test_string() {
32 test(S(""));
33 test(S("abcde"));
34 test(S("abcdefghij"));
35 test(S("abcdefghijklmnopqrst"));
36}
37
38TEST_CONSTEXPR_CXX20 bool test() {
39 test_string<std::string>();
40#if TEST_STD_VER >= 11
41 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
42#endif
43
44 return true;
45}
46
47int main(int, char**) {
48 test();
49#if TEST_STD_VER > 17
50 static_assert(test());
51#endif
52
53 return 0;
54}
55

source code of libcxx/test/std/strings/basic.string/string.ops/string.accessors/c_str.pass.cpp