| 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 | // { dg-require-effective-target hosted } |
| 20 | |
| 21 | #include <memory> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | template class std::allocator<void>; |
| 25 | |
| 26 | void |
| 27 | test01() |
| 28 | { |
| 29 | int i; |
| 30 | using alloc_type = std::allocator<void>; |
| 31 | alloc_type a; |
| 32 | std::allocator_traits<alloc_type>::construct(a, &i, 42); |
| 33 | VERIFY( i == 42 ); |
| 34 | std::allocator_traits<alloc_type>::destroy(a, &i); |
| 35 | } |
| 36 | |
| 37 | static_assert( std::allocator<void>::propagate_on_container_move_assignment(), |
| 38 | "POCMA trait should always be present" ); |
| 39 | #if __cplusplus <= 202302L |
| 40 | static_assert( std::allocator<void>::is_always_equal(), |
| 41 | "is_always_equal trait should be present before C++26" ); |
| 42 | #endif |
| 43 | |
| 44 | static_assert( |
| 45 | std::is_same<std::allocator<void>::size_type, std::size_t>(), |
| 46 | "size_type is size_t" ); |
| 47 | static_assert( |
| 48 | std::is_same<std::allocator<void>::difference_type, std::ptrdiff_t>(), |
| 49 | "size_type is size_t" ); |
| 50 | |
| 51 | // These properties are formally unspecified, but have always been true for |
| 52 | // the libstdc++ definition of allocator<void>. |
| 53 | static_assert( |
| 54 | std::is_trivially_default_constructible<std::allocator<void>>::value, |
| 55 | "explicit specialization has trivial default constructor"); |
| 56 | static_assert( |
| 57 | std::is_trivially_copy_constructible<std::allocator<void>>::value, |
| 58 | "explicit specialization has trivial copy constructor"); |
| 59 | static_assert( |
| 60 | std::is_trivially_move_constructible<std::allocator<void>>::value, |
| 61 | "explicit specialization has trivial move constructor"); |
| 62 | static_assert( |
| 63 | std::is_trivially_destructible<std::allocator<void>>::value, |
| 64 | "explicit specialization has trivial destructor"); |
| 65 | |
| 66 | #if __cplusplus >= 202002L |
| 67 | // C++20 removes the allocator<void> explicit specialization, so it can now be |
| 68 | // constructed using the converting constructor from other specializations. |
| 69 | static_assert( std::is_nothrow_constructible_v<std::allocator<void>, |
| 70 | std::allocator<int>> ); |
| 71 | |
| 72 | template<typename T> |
| 73 | concept has_pointer = requires { typename T::pointer; }; |
| 74 | template<typename T> |
| 75 | concept has_const_pointer = requires { typename T::const_pointer; }; |
| 76 | template<typename T> |
| 77 | concept has_size_type = requires { typename T::size_type; }; |
| 78 | template<typename T> |
| 79 | concept has_difference_type = requires { typename T::difference_type; }; |
| 80 | |
| 81 | // These were removed for C++20 |
| 82 | static_assert( ! has_pointer<std::allocator<void>> ); |
| 83 | static_assert( ! has_const_pointer<std::allocator<void>> ); |
| 84 | |
| 85 | #else |
| 86 | static_assert( |
| 87 | std::is_same<std::allocator<void>::pointer, void*>(), |
| 88 | "pointer is void*" ); |
| 89 | static_assert( std::is_same<std::allocator<void>::const_pointer, const void*>(), |
| 90 | "const_pointer is const void*" ); |
| 91 | #endif // C++20 |
| 92 | |
| 93 | int |
| 94 | main() |
| 95 | { |
| 96 | test01(); |
| 97 | } |