Skip to content

Commit 70de8f2

Browse files
committed
Provide strong exception guarantee for forward_list::resize
1 parent 9d7e1d9 commit 70de8f2

File tree

4 files changed

+138
-32
lines changed

4 files changed

+138
-32
lines changed

libcxx/docs/Status/Cxx2cIssues.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
"`LWG4153 <https://wg21.link/LWG4153>`__","Fix extra ""-1"" for ``philox_engine::max()``","2024-11 (Wrocław)","","",""
108108
"`LWG4154 <https://wg21.link/LWG4154>`__","The Mandates for ``std::packaged_task``'s constructor from a callable entity should consider decaying","2024-11 (Wrocław)","","",""
109109
"`LWG4157 <https://wg21.link/LWG4157>`__","The resolution of LWG3465 was damaged by P2167R3","2024-11 (Wrocław)","","20",""
110-
"`LWG4164 <https://wg21.link/LWG4164>`__","Missing guarantees for ``forward_list`` modifiers","2024-11 (Wrocław)","","",""
110+
"`LWG4164 <https://wg21.link/LWG4164>`__","Missing guarantees for ``forward_list`` modifiers","2024-11 (Wrocław)","|Complete|","21",""
111111
"`LWG4169 <https://wg21.link/LWG4169>`__","``std::atomic<T>``'s default constructor should be constrained","2024-11 (Wrocław)","","",""
112112
"`LWG4170 <https://wg21.link/LWG4170>`__","``contiguous_iterator`` should require ``to_address(I{})``","2024-11 (Wrocław)","","",""
113113
"","","","","",""

libcxx/include/forward_list

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@ public:
819819

820820
template <class _InputIterator, class _Sentinel>
821821
_LIBCPP_HIDE_FROM_ABI iterator __insert_after_with_sentinel(const_iterator __p, _InputIterator __f, _Sentinel __l);
822+
_LIBCPP_HIDE_FROM_ABI iterator __default_insert_after(const_iterator __p, size_type __n);
822823

823824
_LIBCPP_HIDE_FROM_ABI iterator erase_after(const_iterator __p);
824825
_LIBCPP_HIDE_FROM_ABI iterator erase_after(const_iterator __f, const_iterator __l);
@@ -1128,6 +1129,36 @@ forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, const value_type& __
11281129
return iterator(__r->__next_);
11291130
}
11301131

1132+
template <class _Tp, class _Alloc>
1133+
typename forward_list<_Tp, _Alloc>::iterator
1134+
forward_list<_Tp, _Alloc>::__default_insert_after(const_iterator __p, size_type __n) {
1135+
__begin_node_pointer __r = __p.__get_begin();
1136+
if (__n > 0) {
1137+
__node_pointer __first = this->__create_node(/* next = */ nullptr);
1138+
__node_pointer __last = __first;
1139+
# if _LIBCPP_HAS_EXCEPTIONS
1140+
try {
1141+
# endif // _LIBCPP_HAS_EXCEPTIONS
1142+
for (--__n; __n != 0; --__n, __last = __last->__next_) {
1143+
__last->__next_ = this->__create_node(/* next = */ nullptr);
1144+
}
1145+
# if _LIBCPP_HAS_EXCEPTIONS
1146+
} catch (...) {
1147+
while (__first != nullptr) {
1148+
__node_pointer __next = __first->__next_;
1149+
this->__delete_node(__first);
1150+
__first = __next;
1151+
}
1152+
throw;
1153+
}
1154+
# endif // _LIBCPP_HAS_EXCEPTIONS
1155+
__last->__next_ = __r->__next_;
1156+
__r->__next_ = __first;
1157+
__r = static_cast<__begin_node_pointer>(__last);
1158+
}
1159+
return iterator(__r);
1160+
}
1161+
11311162
template <class _Tp, class _Alloc>
11321163
typename forward_list<_Tp, _Alloc>::iterator
11331164
forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, size_type __n, const value_type& __v) {
@@ -1240,12 +1271,7 @@ void forward_list<_Tp, _Alloc>::resize(size_type __n) {
12401271
if (__i != __e)
12411272
erase_after(__p, __e);
12421273
else {
1243-
__n -= __sz;
1244-
if (__n > 0) {
1245-
for (__begin_node_pointer __ptr = __p.__get_begin(); __n > 0; --__n, __ptr = __ptr->__next_as_begin()) {
1246-
__ptr->__next_ = this->__create_node(/* next = */ nullptr);
1247-
}
1248-
}
1274+
__default_insert_after(__p, __n - __sz);
12491275
}
12501276
}
12511277

@@ -1260,12 +1286,7 @@ void forward_list<_Tp, _Alloc>::resize(size_type __n, const value_type& __v) {
12601286
if (__i != __e)
12611287
erase_after(__p, __e);
12621288
else {
1263-
__n -= __sz;
1264-
if (__n > 0) {
1265-
for (__begin_node_pointer __ptr = __p.__get_begin(); __n > 0; --__n, __ptr = __ptr->__next_as_begin()) {
1266-
__ptr->__next_ = this->__create_node(/* next = */ nullptr, __v);
1267-
}
1268-
}
1289+
insert_after(__p, __n - __sz, __v);
12691290
}
12701291
}
12711292

libcxx/test/std/containers/exception_safety_helpers.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,33 @@ int ThrowingCopy<N>::created_by_copying = 0;
5353
template <int N>
5454
int ThrowingCopy<N>::destroyed = 0;
5555

56+
template <int N>
57+
struct ThrowingDefault {
58+
static bool throwing_enabled;
59+
static int default_constructed;
60+
static int destroyed;
61+
int x = 0;
62+
63+
ThrowingDefault() {
64+
++default_constructed;
65+
if (throwing_enabled && default_constructed == N) {
66+
throw -1;
67+
}
68+
}
69+
70+
ThrowingDefault(int value) : x(value) {}
71+
ThrowingDefault(const ThrowingDefault& other) = default;
72+
friend bool operator==(const ThrowingDefault& lhs, const ThrowingDefault& rhs) { return lhs.x == rhs.x; }
73+
friend bool operator<(const ThrowingDefault& lhs, const ThrowingDefault& rhs) { return lhs.x < rhs.x; }
74+
75+
static void reset() { default_constructed = destroyed = 0; }
76+
};
77+
78+
template <int N>
79+
bool ThrowingDefault<N>::throwing_enabled = true;
80+
template <int N>
81+
int ThrowingDefault<N>::default_constructed = 0;
82+
5683
template <int N>
5784
struct std::hash<ThrowingCopy<N>> {
5885
std::size_t operator()(const ThrowingCopy<N>& value) const { return value.x; }
@@ -95,6 +122,29 @@ void test_exception_safety_throwing_copy_container(Func&& func) {
95122
}
96123
}
97124

125+
template <int ThrowOn, int Size, class Func>
126+
void test_strong_exception_safety_throwing_copy(Func&& func) {
127+
using T = ThrowingCopy<ThrowOn>;
128+
T::throwing_enabled = false;
129+
130+
std::forward_list<T> c0(Size);
131+
for (int i = 0; i < Size; ++i)
132+
c0.emplace_front(i);
133+
std::forward_list<T> c = c0;
134+
T in[Size];
135+
T::reset();
136+
T::throwing_enabled = true;
137+
try {
138+
func(c, in, in + Size);
139+
assert(false); // The function call above should throw.
140+
141+
} catch (int) {
142+
assert(T::created_by_copying == ThrowOn);
143+
assert(T::destroyed == ThrowOn - 1); // No destructor call for the partially-constructed element.
144+
assert(c == c0); // Strong exception guarantee
145+
}
146+
}
147+
98148
#endif // !defined(TEST_HAS_NO_EXCEPTIONS)
99149

100150
#endif // SUPPORT_EXCEPTION_SAFETY_HELPERS_H

libcxx/test/std/containers/sequences/forwardlist/exception_safety.pass.cpp

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,23 @@
3535
// void assign_range(R&& rg); // C++23
3636
//
3737
// void push_front(const value_type& v);
38-
// template<container-compatible-range<T> R>
38+
// template <class... Args> reference emplace_front(Args&&... args); // reference in C++17
39+
// template<container-compatible-range<T> R>
3940
// void prepend_range(R&& rg); // C++23
4041
//
4142
// iterator insert_after(const_iterator p, const value_type& v);
43+
// iterator insert_after(const_iterator p, value_type&& v);
4244
// iterator insert_after(const_iterator p, size_type n, const value_type& v);
45+
// iterator insert_after(const_iterator p, initializer_list<value_type> il);
4346
// template <class InputIterator>
4447
// iterator insert_after(const_iterator p,
4548
// InputIterator first, InputIterator last);
46-
// template<container-compatible-range<T> R>
49+
// template<container-compatible-range<T> R>
4750
// iterator insert_range_after(const_iterator position, R&& rg); // C++23
48-
//
51+
// template <class... Args>
52+
// iterator emplace_after(const_iterator p, Args&&... args);
4953
// void resize(size_type n, const value_type& v);
54+
// void resize(size_type n);
5055

5156
#include <forward_list>
5257

@@ -65,16 +70,33 @@ int main(int, char**) {
6570
using T = ThrowingCopy<ThrowOn>;
6671

6772
// void push_front(const value_type& v);
68-
test_exception_safety_throwing_copy<ThrowOn, Size>([](T* from, T*) {
69-
std::forward_list<T> c;
73+
test_strong_exception_safety_throwing_copy<ThrowOn, Size>([](std::forward_list<T>& c, T* from, T*) {
74+
c.push_front(*from);
75+
});
76+
77+
// template <class... Args> reference emplace_front(Args&&... args);
78+
test_strong_exception_safety_throwing_copy<ThrowOn, Size>([](std::forward_list<T>& c, T* from, T*) {
7079
c.push_front(*from);
7180
});
7281

7382
// iterator insert_after(const_iterator p, const value_type& v);
74-
test_exception_safety_throwing_copy</*ThrowOn=*/1, Size>([](T* from, T*) {
75-
std::forward_list<T> c;
83+
test_strong_exception_safety_throwing_copy<ThrowOn, Size>([](std::forward_list<T>& c, T* from, T*) {
7684
c.insert_after(c.before_begin(), *from);
7785
});
86+
87+
// iterator insert_after(const_iterator p, value_type&& v);
88+
test_strong_exception_safety_throwing_copy<ThrowOn, Size>([](std::forward_list<T>& c, T* from, T*) {
89+
c.insert_after(c.before_begin(), std::move(*from));
90+
});
91+
92+
// template <class... Args>
93+
// iterator emplace_after(const_iterator p, Args&&... args);
94+
test_strong_exception_safety_throwing_copy<ThrowOn, Size>([](std::forward_list<T>& c, T* from, T*) {
95+
c.emplace_after(c.before_begin(), *from);
96+
});
97+
test_strong_exception_safety_throwing_copy<ThrowOn, Size>([](std::forward_list<T>& c, T* from, T*) {
98+
c.emplace_after(c.before_begin(), std::move(*from));
99+
});
78100
}
79101

80102
{
@@ -169,40 +191,53 @@ int main(int, char**) {
169191
#if TEST_STD_VER >= 23
170192
// template<container-compatible-range<T> R>
171193
// void prepend_range(R&& rg); // C++23
172-
test_exception_safety_throwing_copy<ThrowOn, Size>([](T* from, T* to) {
173-
std::forward_list<T> c;
194+
test_strong_exception_safety_throwing_copy<ThrowOn, Size>([](std::forward_list<T>& c, T* from, T* to) {
174195
c.prepend_range(std::ranges::subrange(from, to));
175196
});
176197
#endif
177198

178199
// iterator insert_after(const_iterator p, size_type n, const value_type& v);
179-
test_exception_safety_throwing_copy<ThrowOn, Size>([](T* from, T*) {
180-
std::forward_list<T> c;
200+
test_strong_exception_safety_throwing_copy<ThrowOn, Size>([](std::forward_list<T>& c, T* from, T*) {
181201
c.insert_after(c.before_begin(), Size, *from);
182202
});
183203

184204
// template <class InputIterator>
185205
// iterator insert_after(const_iterator p,
186206
// InputIterator first, InputIterator last);
187-
test_exception_safety_throwing_copy<ThrowOn, Size>([](T* from, T* to) {
188-
std::forward_list<T> c;
207+
test_strong_exception_safety_throwing_copy<ThrowOn, Size>([](std::forward_list<T>& c, T* from, T* to) {
189208
c.insert_after(c.before_begin(), from, to);
190209
});
191210

211+
// iterator insert_after(const_iterator p, initializer_list<value_type> il);
212+
std::initializer_list<T> il{1, 2, 3, 4, 5};
213+
test_strong_exception_safety_throwing_copy<ThrowOn, Size>([&](std::forward_list<T>& c, T*, T*) {
214+
c.insert_after(c.before_begin(), il);
215+
});
216+
192217
#if TEST_STD_VER >= 23
193218
// template<container-compatible-range<T> R>
194219
// iterator insert_range_after(const_iterator position, R&& rg); // C++23
195-
test_exception_safety_throwing_copy<ThrowOn, Size>([](T* from, T* to) {
196-
std::forward_list<T> c;
220+
test_strong_exception_safety_throwing_copy<ThrowOn, Size>([](std::forward_list<T>& c, T* from, T* to) {
197221
c.insert_range_after(c.before_begin(), std::ranges::subrange(from, to));
198222
});
199223
#endif
200224

201225
// void resize(size_type n, const value_type& v);
202-
test_exception_safety_throwing_copy<ThrowOn, Size>([](T* from, T*) {
203-
std::forward_list<T> c;
204-
c.resize(Size, *from);
205-
});
226+
test_strong_exception_safety_throwing_copy<ThrowOn, Size>([](std::forward_list<T>& c, T* from, T*) {
227+
c.resize(Size * 3, *from);
228+
});
229+
230+
{ // void resize(size_type n);
231+
using X = ThrowingDefault<ThrowOn>;
232+
std::forward_list<X> c0{X{1}, X{2}, X{3}};
233+
std::forward_list<X> c = c0;
234+
try {
235+
c.resize(3 * ThrowOn);
236+
assert(false);
237+
} catch (int) {
238+
assert(c == c0);
239+
}
240+
}
206241
}
207242

208243
return 0;

0 commit comments

Comments
 (0)