| 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 | 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. |
| 31 | std::string unused_string; |
| 32 | #ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 33 | std::wstring unused_wide_string; |
| 34 | #endif |
| 35 | |
| 36 | static_assert(std::is_nothrow_destructible<std::string>::value, "" ); |
| 37 | static_assert( |
| 38 | std::is_nothrow_destructible< std::basic_string<char, std::char_traits<char>, test_allocator<char>>>::value, "" ); |
| 39 | LIBCPP_STATIC_ASSERT( |
| 40 | !std::is_nothrow_destructible< std::basic_string<char, std::char_traits<char>, throwing_alloc<char>>>::value, "" ); |
| 41 | |
| 42 | TEST_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 | |
| 55 | int main(int, char**) { |
| 56 | test(); |
| 57 | #if TEST_STD_VER > 17 |
| 58 | static_assert(test()); |
| 59 | #endif |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |