xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/dtor.pass.cpp (revision 425620ccdd47e56b59512913cdc71e116f951e4e)
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 
21 template <class T>
22 struct throwing_alloc
23 {
24     typedef T value_type;
25     throwing_alloc(const throwing_alloc&);
26     T *allocate(size_t);
27     ~throwing_alloc() noexcept(false);
28 };
29 
30 // Test that it's possible to take the address of basic_string's destructors
31 // by creating globals which will register their destructors with cxa_atexit.
32 std::string s;
33 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
34 std::wstring ws;
35 #endif
36 
37 static_assert(std::is_nothrow_destructible<std::string>::value, "");
38 static_assert(std::is_nothrow_destructible<
39                 std::basic_string<char, std::char_traits<char>, test_allocator<char>>>::value, "");
40 LIBCPP_STATIC_ASSERT(!std::is_nothrow_destructible<
41                      std::basic_string<char, std::char_traits<char>, throwing_alloc<char>>>::value, "");
42 
43 TEST_CONSTEXPR_CXX20 bool test() {
44   test_allocator_statistics alloc_stats;
45   {
46     std::basic_string<char, std::char_traits<char>, test_allocator<char>> str2((test_allocator<char>(&alloc_stats)));
47     str2 = "long long string so no SSO";
48     assert(alloc_stats.alloc_count == 1);
49   }
50   assert(alloc_stats.alloc_count == 0);
51 
52   return true;
53 }
54 
55 int main(int, char**)
56 {
57   test();
58 #if TEST_STD_VER > 17
59   static_assert(test());
60 #endif
61 
62   return 0;
63 }
64