Skip to content

Commit 8f16d4f

Browse files
committed
workaround for gcc bug with placement new: v7
1 parent 2aada1a commit 8f16d4f

File tree

1 file changed

+17
-33
lines changed

1 file changed

+17
-33
lines changed

libcxx/include/__algorithm/stable_sort.h

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,12 @@ _LIBCPP_BEGIN_NAMESPACE_STD
3737

3838
// Workaround for "constexpr placement new" bug in gcc (fixed in 14.2).
3939
// See https://github.com/llvm/llvm-project/pull/110320#discussion_r1788557715.
40-
#define __STABLE_SORT_NEW_IMPL(__placement_arg, __type, __new_initializer_func, __new_initializer_arg) \
41-
do { \
42-
::new ((void*)__placement_arg) __type(__new_initializer_func(__new_initializer_arg)); \
43-
} while (0)
4440
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 <= 140100)
45-
# define __STABLE_SORT_NEW(__placement_arg, __type, __new_initializer_func, __new_initializer_arg) \
46-
do { \
47-
[__placement_arg, &__new_initializer_arg] { \
48-
__STABLE_SORT_NEW_IMPL(__placement_arg, __type, __new_initializer_func, __new_initializer_arg); \
49-
}(); \
50-
} while (0)
41+
# define _LIBCPP_MOVING_PLACEMENT_NEW(__ptr, __type, __move_func, __iter) \
42+
[__ptr, &__iter] { ::new ((void*)__ptr) __type(__move_func(__iter)); }()
5143
#else
52-
# define __STABLE_SORT_NEW(__placement_arg, __type, __new_initializer_func, __new_initializer_arg) \
53-
do { \
54-
__STABLE_SORT_NEW_IMPL(__placement_arg, __type, __new_initializer_func, __new_initializer_arg); \
55-
} while (0)
44+
# define _LIBCPP_MOVING_PLACEMENT_NEW(__ptr, __type, __move_func, __iter) \
45+
::new ((void*)__ptr) __type(__move_func(__iter))
5646
#endif
5747

5848
template <class _AlgPolicy, class _Compare, class _BidirectionalIterator>
@@ -68,22 +58,22 @@ _LIBCPP_HIDE_FROM_ABI void __insertion_sort_move(
6858
__destruct_n __d(0);
6959
unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
7060
value_type* __last2 = __first2;
71-
__STABLE_SORT_NEW(__last2, value_type, _Ops::__iter_move, __first1);
61+
_LIBCPP_MOVING_PLACEMENT_NEW(__last2, value_type, _Ops::__iter_move, __first1);
7262
__d.template __incr<value_type>();
7363
for (++__last2; ++__first1 != __last1; ++__last2) {
7464
value_type* __j2 = __last2;
7565
value_type* __i2 = __j2;
7666
if (__comp(*__first1, *--__i2)) {
7767
{
7868
value_type& __tmp = *__i2;
79-
__STABLE_SORT_NEW(__j2, value_type, std::move, __tmp);
69+
_LIBCPP_MOVING_PLACEMENT_NEW(__j2, value_type, std::move, __tmp);
8070
}
8171
__d.template __incr<value_type>();
8272
for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
8373
*__j2 = std::move(*__i2);
8474
*__j2 = _Ops::__iter_move(__first1);
8575
} else {
86-
__STABLE_SORT_NEW(__j2, value_type, _Ops::__iter_move, __first1);
76+
_LIBCPP_MOVING_PLACEMENT_NEW(__j2, value_type, _Ops::__iter_move, __first1);
8777
__d.template __incr<value_type>();
8878
}
8979
}
@@ -107,27 +97,22 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __merge_move_construct(
10797
for (; true; ++__result) {
10898
if (__first1 == __last1) {
10999
for (; __first2 != __last2; ++__first2, (void)++__result, __d.template __incr<value_type>())
110-
__STABLE_SORT_NEW(__result, value_type, _Ops::__iter_move, __first2);
100+
_LIBCPP_MOVING_PLACEMENT_NEW(__result, value_type, _Ops::__iter_move, __first2);
111101
__h.release();
112102
return;
113103
}
114104
if (__first2 == __last2) {
115105
for (; __first1 != __last1; ++__first1, (void)++__result, __d.template __incr<value_type>())
116-
__STABLE_SORT_NEW(__result, value_type, _Ops::__iter_move, __first1);
106+
_LIBCPP_MOVING_PLACEMENT_NEW(__result, value_type, _Ops::__iter_move, __first1);
117107
__h.release();
118108
return;
119109
}
120110
if (__comp(*__first2, *__first1)) {
121-
__STABLE_SORT_NEW(__result, value_type, _Ops::__iter_move, __first2);
111+
_LIBCPP_MOVING_PLACEMENT_NEW(__result, value_type, _Ops::__iter_move, __first2);
122112
__d.template __incr<value_type>();
123113
++__first2;
124114
} else {
125-
// __STABLE_SORT_NEW(__result, value_type, _Ops::__iter_move, __first1);
126-
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 <= 140100)
127-
[__result, &__first1] { ::new ((void*)__result) value_type(_Ops::__iter_move(__first1)); }();
128-
#else
129-
::new ((void*)__result) value_type(_Ops::__iter_move(__first1));
130-
#endif
115+
_LIBCPP_MOVING_PLACEMENT_NEW(__result, value_type, _Ops::__iter_move, __first1);
131116
__d.template __incr<value_type>();
132117
++__first1;
133118
}
@@ -185,21 +170,21 @@ _LIBCPP_CONSTEXPR_SINCE_CXX26 void __stable_sort_move(
185170
case 0:
186171
return;
187172
case 1:
188-
__STABLE_SORT_NEW(__first2, value_type, _Ops::__iter_move, __first1);
173+
_LIBCPP_MOVING_PLACEMENT_NEW(__first2, value_type, _Ops::__iter_move, __first1);
189174
return;
190175
case 2:
191176
__destruct_n __d(0);
192177
unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
193178
if (__comp(*--__last1, *__first1)) {
194-
__STABLE_SORT_NEW(__first2, value_type, _Ops::__iter_move, __last1);
179+
_LIBCPP_MOVING_PLACEMENT_NEW(__first2, value_type, _Ops::__iter_move, __last1);
195180
__d.template __incr<value_type>();
196181
++__first2;
197-
__STABLE_SORT_NEW(__first2, value_type, _Ops::__iter_move, __first1);
182+
_LIBCPP_MOVING_PLACEMENT_NEW(__first2, value_type, _Ops::__iter_move, __first1);
198183
} else {
199-
__STABLE_SORT_NEW(__first2, value_type, _Ops::__iter_move, __first1);
184+
_LIBCPP_MOVING_PLACEMENT_NEW(__first2, value_type, _Ops::__iter_move, __first1);
200185
__d.template __incr<value_type>();
201186
++__first2;
202-
__STABLE_SORT_NEW(__first2, value_type, _Ops::__iter_move, __last1);
187+
_LIBCPP_MOVING_PLACEMENT_NEW(__first2, value_type, _Ops::__iter_move, __last1);
203188
}
204189
__h2.release();
205190
return;
@@ -297,8 +282,7 @@ stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) {
297282
std::stable_sort(__first, __last, __less<>());
298283
}
299284

300-
#undef __STABLE_SORT_NEW
301-
#undef __STABLE_SORT_NEW_IMPL
285+
#undef _LIBCPP_MOVING_PLACEMENT_NEW
302286
_LIBCPP_END_NAMESPACE_STD
303287
_LIBCPP_POP_MACROS
304288

0 commit comments

Comments
 (0)