| 1 | // Copyright (C) 2019-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++20 } } |
| 19 | |
| 20 | // P0591R4 makes uses-allocator construction apply recursively for nested pairs |
| 21 | |
| 22 | #include <scoped_allocator> |
| 23 | #include <vector> |
| 24 | #include <testsuite_hooks.h> |
| 25 | #include <testsuite_allocator.h> |
| 26 | |
| 27 | struct X |
| 28 | { |
| 29 | using allocator_type = __gnu_test::uneq_allocator<int>; |
| 30 | |
| 31 | X(int personality) : a(personality) { } |
| 32 | X(std::allocator_arg_t, allocator_type a) : a(a) { } |
| 33 | X(std::allocator_arg_t, allocator_type a, const X&) : a(a) { } |
| 34 | |
| 35 | allocator_type a; |
| 36 | }; |
| 37 | |
| 38 | void |
| 39 | test01() |
| 40 | { |
| 41 | using value_type = std::pair<std::pair<X, int>, std::pair<int, X>>; |
| 42 | using scoped_alloc |
| 43 | = std::scoped_allocator_adaptor<__gnu_test::uneq_allocator<value_type>>; |
| 44 | |
| 45 | const scoped_alloc a(10); |
| 46 | std::vector<value_type, scoped_alloc> v(a); |
| 47 | VERIFY( v.get_allocator().get_personality() == a.get_personality() ); |
| 48 | |
| 49 | value_type val( { X(1), 2 }, { 3, X(4) } ); |
| 50 | v.push_back(val); |
| 51 | X& x1 = v.back().first.first; |
| 52 | VERIFY( x1.a.get_personality() != val.first.first.a.get_personality() ); |
| 53 | VERIFY( x1.a.get_personality() == a.get_personality() ); |
| 54 | |
| 55 | X& x2 = v.back().second.second; |
| 56 | VERIFY( x2.a.get_personality() != val.second.second.a.get_personality() ); |
| 57 | VERIFY( x2.a.get_personality() == a.get_personality() ); |
| 58 | |
| 59 | // Check other members of the pairs are correctly initialized too: |
| 60 | VERIFY( v.back().first.second == val.first.second ); |
| 61 | VERIFY( v.back().second.first == val.second.first ); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | test02() |
| 66 | { |
| 67 | using value_type = std::pair<std::pair<X, int>, std::pair<int, X>>; |
| 68 | using scoped_alloc |
| 69 | = std::scoped_allocator_adaptor<__gnu_test::uneq_allocator<value_type>, |
| 70 | X::allocator_type>; |
| 71 | |
| 72 | const scoped_alloc a(10, 20); |
| 73 | std::vector<value_type, scoped_alloc> v(a); |
| 74 | VERIFY( v.get_allocator().get_personality() == a.get_personality() ); |
| 75 | |
| 76 | value_type val( { X(1), 2 }, { 3, X(4) } ); |
| 77 | v.push_back(val); |
| 78 | X& x1 = v.back().first.first; |
| 79 | VERIFY( x1.a.get_personality() != val.first.first.a.get_personality() ); |
| 80 | VERIFY( x1.a.get_personality() != a.get_personality() ); |
| 81 | VERIFY( x1.a.get_personality() == a.inner_allocator().get_personality() ); |
| 82 | |
| 83 | X& x2 = v.back().second.second; |
| 84 | VERIFY( x2.a.get_personality() != val.second.second.a.get_personality() ); |
| 85 | VERIFY( x2.a.get_personality() != a.get_personality() ); |
| 86 | VERIFY( x2.a.get_personality() == a.inner_allocator().get_personality() ); |
| 87 | |
| 88 | // Check other members of the pairs are correctly initialized too: |
| 89 | VERIFY( v.back().first.second == val.first.second ); |
| 90 | VERIFY( v.back().second.first == val.second.first ); |
| 91 | } |
| 92 | |
| 93 | int |
| 94 | main() |
| 95 | { |
| 96 | test01(); |
| 97 | test02(); |
| 98 | } |