brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.8 KiB · d5c1f4d Raw
64 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++0310 11// <string>12 13// ~basic_string() // implied noexcept; // constexpr since C++2014 15#include <cassert>16#include <string>17#include <type_traits>18 19#include "test_allocator.h"20#include "test_macros.h"21 22template <class T>23struct throwing_alloc {24  typedef T value_type;25  throwing_alloc(const throwing_alloc&);26  T* allocate(std::size_t);27  ~throwing_alloc() noexcept(false);28};29 30// Test that it's possible to take the address of basic_string's destructors31// by creating globals which will register their destructors with cxa_atexit.32std::string unused_string;33#ifndef TEST_HAS_NO_WIDE_CHARACTERS34std::wstring unused_wide_string;35#endif36 37static_assert(std::is_nothrow_destructible<std::string>::value, "");38static_assert(39    std::is_nothrow_destructible< std::basic_string<char, std::char_traits<char>, test_allocator<char>>>::value, "");40LIBCPP_STATIC_ASSERT(41    !std::is_nothrow_destructible< std::basic_string<char, std::char_traits<char>, throwing_alloc<char>>>::value, "");42 43TEST_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 > 0);49    LIBCPP_ASSERT(alloc_stats.alloc_count == 1);50  }51  assert(alloc_stats.alloc_count == 0);52 53  return true;54}55 56int main(int, char**) {57  test();58#if TEST_STD_VER > 1759  static_assert(test());60#endif61 62  return 0;63}64