| 1 |
// Copyright (C) 2018-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do run { target c++11 } } |
| 19 |
|
| 20 |
// PR libstdc++/87749 |
| 21 |
|
| 22 |
#include <string> |
| 23 |
#include <testsuite_hooks.h> |
| 24 |
|
| 25 |
bool oom = false; |
| 26 |
|
| 27 |
template<typename T> |
| 28 |
struct alloc |
| 29 |
{ |
| 30 |
using value_type = T; |
| 31 |
|
| 32 |
#if !_GLIBCXX_USE_CXX11_ABI |
| 33 |
using size_type = unsigned long; |
| 34 |
using difference_type = long; |
| 35 |
using reference = T&; |
| 36 |
using const_reference = T&; |
| 37 |
using pointer = T*; |
| 38 |
using const_pointer = const T*; |
| 39 |
template<typename U> |
| 40 |
struct rebind { using other = alloc<U>; }; |
| 41 |
#endif |
| 42 |
|
| 43 |
int not_empty = 0; // this makes is_always_equal false |
| 44 |
|
| 45 |
alloc() = default; |
| 46 |
template<typename U> |
| 47 |
alloc(const alloc<U>&) { } |
| 48 |
|
| 49 |
T* allocate(unsigned long n) |
| 50 |
{ |
| 51 |
if (oom) |
| 52 |
throw std::bad_alloc(); |
| 53 |
return std::allocator<T>().allocate(n); |
| 54 |
} |
| 55 |
|
| 56 |
void deallocate(T* p, unsigned long n) |
| 57 |
{ |
| 58 |
std::allocator<T>().deallocate(p, n); |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
template<typename T, typename U> |
| 63 |
bool operator==(const alloc<T>&, const alloc<U>&) { return true; } |
| 64 |
|
| 65 |
template<typename T, typename U> |
| 66 |
bool operator!=(const alloc<T>&, const alloc<U>&) { return false; } |
| 67 |
|
| 68 |
int main() |
| 69 |
{ |
| 70 |
using string |
| 71 |
= std::basic_string<wchar_t, std::char_traits<wchar_t>, alloc<wchar_t>>; |
| 72 |
|
| 73 |
string s = L"PR libstdc++/87749 a string that is longer than a short string"; |
| 74 |
const auto ptr = s.c_str(); |
| 75 |
string ss; |
| 76 |
oom = true; |
| 77 |
ss = std::move(s); // allocators are equal, should not allocate new storage |
| 78 |
VERIFY( ss.c_str() == ptr ); |
| 79 |
} |