| 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++14 } } |
| 19 | |
| 20 | #include <vector> |
| 21 | #include <memory> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | // See https://gcc.gnu.org/ml/libstdc++/2016-07/msg00008.html for background. |
| 25 | |
| 26 | struct T |
| 27 | { |
| 28 | T(int v = 0) : value(v) { } |
| 29 | T(const T& t); |
| 30 | T& operator=(const T& t); |
| 31 | void make_child() { child = std::make_unique<T>(value + 10); } |
| 32 | std::unique_ptr<T> child; |
| 33 | int value; |
| 34 | }; |
| 35 | |
| 36 | T::T(const T& t) : value(t.value) |
| 37 | { |
| 38 | if (t.child) |
| 39 | child.reset(new T(*t.child)); |
| 40 | } |
| 41 | |
| 42 | T& T::operator=(const T& t) |
| 43 | { |
| 44 | value = t.value; |
| 45 | if (t.child) |
| 46 | { |
| 47 | if (child) |
| 48 | *child = *t.child; |
| 49 | else |
| 50 | child.reset(new T(*t.child)); |
| 51 | } |
| 52 | else |
| 53 | child.reset(); |
| 54 | return *this; |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | test01() |
| 59 | { |
| 60 | std::vector<T> v; |
| 61 | v.reserve(3); |
| 62 | v.push_back(T(1)); |
| 63 | v.back().make_child(); |
| 64 | v.push_back(T(2)); |
| 65 | v.back().make_child(); |
| 66 | |
| 67 | VERIFY(v[1].child->value == 12); |
| 68 | VERIFY(v[1].child->child == nullptr); |
| 69 | |
| 70 | v.insert(v.begin(), *v[1].child); |
| 71 | |
| 72 | VERIFY(v[0].value == 12); |
| 73 | VERIFY(v[0].child == nullptr); |
| 74 | } |
| 75 | |
| 76 | int main() |
| 77 | { |
| 78 | test01(); |
| 79 | } |