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// UNSUPPORTED: c++03
10
11// <string>
12
13// ~basic_string() // implied noexcept; // constexpr since C++20
14
15#include <string>
16#include <cassert>
17
18#include "test_macros.h"
19#include "test_allocator.h"
20
21template <class T>
22struct throwing_alloc {
23 typedef T value_type;
24 throwing_alloc(const throwing_alloc&);
25 T* allocate(std::size_t);
26 ~throwing_alloc() noexcept(false);
27};
28
29// Test that it's possible to take the address of basic_string's destructors
30// by creating globals which will register their destructors with cxa_atexit.
31std::string unused_string;
32#ifndef TEST_HAS_NO_WIDE_CHARACTERS
33std::wstring unused_wide_string;
34#endif
35
36static_assert(std::is_nothrow_destructible<std::string>::value, "");
37static_assert(
38 std::is_nothrow_destructible< std::basic_string<char, std::char_traits<char>, test_allocator<char>>>::value, "");
39LIBCPP_STATIC_ASSERT(
40 !std::is_nothrow_destructible< std::basic_string<char, std::char_traits<char>, throwing_alloc<char>>>::value, "");
41
42TEST_CONSTEXPR_CXX20 bool test() {
43 test_allocator_statistics alloc_stats;
44 {
45 std::basic_string<char, std::char_traits<char>, test_allocator<char>> str2((test_allocator<char>(&alloc_stats)));
46 str2 = "long long string so no SSO";
47 assert(alloc_stats.alloc_count > 0);
48 LIBCPP_ASSERT(alloc_stats.alloc_count == 1);
49 }
50 assert(alloc_stats.alloc_count == 0);
51
52 return true;
53}
54
55int main(int, char**) {
56 test();
57#if TEST_STD_VER > 17
58 static_assert(test());
59#endif
60
61 return 0;
62}
63

source code of libcxx/test/std/strings/basic.string/string.cons/dtor.pass.cpp