| 1 | // Copyright (C) 2016-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 | #include <string> |
| 21 | #include <testsuite_hooks.h> |
| 22 | |
| 23 | template<typename T> |
| 24 | struct mv_allocator |
| 25 | { |
| 26 | using value_type = T; |
| 27 | using size_type = unsigned; |
| 28 | |
| 29 | mv_allocator() |
| 30 | : moved_to(false), moved_from(false) { } |
| 31 | |
| 32 | template<typename U> |
| 33 | mv_allocator(const mv_allocator<U> & a) |
| 34 | : moved_to(a.moved_to), moved_from(a.moved_from) { } |
| 35 | |
| 36 | mv_allocator(const mv_allocator &) = default; |
| 37 | |
| 38 | mv_allocator(mv_allocator && a) noexcept : moved_to(true) |
| 39 | { |
| 40 | a.moved_from = true; |
| 41 | } |
| 42 | |
| 43 | T* allocate(unsigned n) { return std::allocator<T>{}.allocate(n); } |
| 44 | void deallocate(T* p, unsigned n) { std::allocator<T>{}.deallocate(p, n); } |
| 45 | |
| 46 | bool moved_to; |
| 47 | bool moved_from; |
| 48 | |
| 49 | #if ! _GLIBCXX_USE_CXX11_ABI |
| 50 | // COW string doesn't use allocator_traits, requires C++03 allocator API. |
| 51 | using pointer = T*; |
| 52 | using const_pointer = const T*; |
| 53 | using difference_type = int; |
| 54 | template<typename U> struct rebind { using other = mv_allocator<U>; }; |
| 55 | void construct(pointer p, const T& val) { ::new(p) T(val); } |
| 56 | void destroy(pointer p) { p->~T(); } |
| 57 | size_type max_size() const { return std::allocator<T>().max_size(); } |
| 58 | #endif |
| 59 | }; |
| 60 | |
| 61 | template<typename T, typename U> |
| 62 | bool |
| 63 | operator==(const mv_allocator<T>&, const mv_allocator<U>&) { return true; } |
| 64 | |
| 65 | template<typename T, typename U> |
| 66 | bool |
| 67 | operator!=(const mv_allocator<T>&, const mv_allocator<U>&) { return false; } |
| 68 | |
| 69 | void |
| 70 | test01() |
| 71 | { |
| 72 | std::basic_string<char, std::char_traits<char>, mv_allocator<char>> s; |
| 73 | auto t = std::move(s); |
| 74 | VERIFY( s.get_allocator().moved_from ); |
| 75 | VERIFY( t.get_allocator().moved_to ); |
| 76 | } |
| 77 | |
| 78 | int |
| 79 | main() |
| 80 | { |
| 81 | test01(); |
| 82 | } |