| 1 | // { dg-do run { target c++11 } } |
| 2 | |
| 3 | // Copyright (C) 2011-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // You should have received a copy of the GNU General Public License along |
| 17 | // with this library; see the file COPYING3. If not see |
| 18 | // <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <cstddef> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | struct X { }; |
| 25 | |
| 26 | template<typename T> |
| 27 | struct hintable_allocator |
| 28 | { |
| 29 | typedef T value_type; |
| 30 | struct const_void_pointer { }; |
| 31 | typedef unsigned size_type; |
| 32 | |
| 33 | hintable_allocator() : called(false) { } |
| 34 | |
| 35 | bool called; |
| 36 | |
| 37 | // this is the overload that should get called: |
| 38 | T* allocate(size_type n, const_void_pointer) { called = true; return 0; } |
| 39 | |
| 40 | // none of these should get called: |
| 41 | T* allocate(size_type n); |
| 42 | T* allocate(size_type n, void*); |
| 43 | T* allocate(size_type n, const void*); |
| 44 | T* allocate(size_type n, const_void_pointer) const; |
| 45 | }; |
| 46 | |
| 47 | void test01() |
| 48 | { |
| 49 | typedef std::allocator_traits<hintable_allocator<X>> traits_type; |
| 50 | traits_type::allocator_type a; |
| 51 | traits_type::const_void_pointer v; |
| 52 | X* p __attribute__((unused)) = traits_type::allocate(a, 1, v); |
| 53 | VERIFY( a.called ); |
| 54 | } |
| 55 | |
| 56 | template<typename T> |
| 57 | struct unhintable_allocator |
| 58 | { |
| 59 | typedef T value_type; |
| 60 | typedef unsigned size_type; |
| 61 | |
| 62 | unhintable_allocator() : called(false) { } |
| 63 | |
| 64 | bool called; |
| 65 | |
| 66 | // this is the overload that should get called: |
| 67 | T* allocate(size_type n) { called = true; return 0; } |
| 68 | |
| 69 | // this should not get called: |
| 70 | T* allocate(size_type n, void*); |
| 71 | }; |
| 72 | |
| 73 | void test02() |
| 74 | { |
| 75 | typedef std::allocator_traits<unhintable_allocator<X>> traits_type; |
| 76 | traits_type::allocator_type a; |
| 77 | traits_type::const_void_pointer v; |
| 78 | X* p __attribute__((unused)) = traits_type::allocate(a, 1, v); |
| 79 | VERIFY( a.called ); |
| 80 | } |
| 81 | |
| 82 | int main() |
| 83 | { |
| 84 | test01(); |
| 85 | test02(); |
| 86 | } |