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// This test checks that we can explicitly instantiate std::string with a custom
10// character type and traits and then use `shrink_to_fit`. In particular, this is
11// a regression test for the bug that was reported at https://stackoverflow.com/q/69520633/627587
12// and https://seedcentral.apple.com/sm/feedback_collector/radar/85053279.
13
14// RUN: %{cxx} %{flags} %{compile_flags} %s %{link_flags} -DTU1 -c -o %t.tu1.o
15// RUN: %{cxx} %{flags} %{compile_flags} %s %{link_flags} -DTU2 -c -o %t.tu2.o
16// RUN: %{cxx} %{flags} %t.tu1.o %t.tu2.o %{link_flags} -o %t.exe
17
18// UNSUPPORTED: no-localization
19
20#include <cstdint>
21#include <ios>
22#include <string>
23
24typedef std::uint16_t char16;
25
26struct string16_char_traits {
27 typedef char16 char_type;
28 typedef int int_type;
29
30 typedef std::streamoff off_type;
31 typedef std::mbstate_t state_type;
32 typedef std::fpos<state_type> pos_type;
33
34 static void assign(char_type&, const char_type&) {}
35 static bool eq(const char_type&, const char_type&) { return false; }
36 static bool lt(const char_type&, const char_type&) { return false; }
37 static int compare(const char_type*, const char_type*, std::size_t) { return 0; }
38 static std::size_t length(const char_type*) { return 0; }
39 static const char_type* find(const char_type*, std::size_t, const char_type&) { return nullptr; }
40 static char_type* move(char_type*, const char_type*, std::size_t) { return nullptr; }
41 static char_type* copy(char_type*, const char_type*, std::size_t) { return nullptr; }
42 static char_type* assign(char_type*, std::size_t, char_type) { return nullptr; }
43 static int_type not_eof(const int_type&) { return 0; }
44 static char_type to_char_type(const int_type&) { return char_type(); }
45 static int_type to_int_type(const char_type&) { return int_type(); }
46 static bool eq_int_type(const int_type&, const int_type&) { return false; }
47 static int_type eof() { return int_type(); }
48};
49
50#if defined(TU1)
51template class std::basic_string<char16, string16_char_traits>;
52#else
53extern template class std::basic_string<char16, string16_char_traits>;
54
55int main(int, char**) {
56 std::basic_string<char16, string16_char_traits> s;
57 s.shrink_to_fit();
58}
59#endif
60

source code of libcxx/test/std/strings/basic.string/string.capacity/shrink_to_fit.explicit_instantiation.sh.cpp