Skip to content

Commit 06cf0ce

Browse files
committed
[libc++] Enable move semantics for vector in C++03
We require move semantics in C++03 anyways, so let's enable them for the containers. Reviewed By: ldionne, #libc Spies: libcxx-commits Differential Revision: https://reviews.llvm.org/D123802
1 parent 5f4541f commit 06cf0ce

File tree

20 files changed

+93
-164
lines changed

20 files changed

+93
-164
lines changed

libcxx/include/__iterator/move_iterator.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,12 @@ class _LIBCPP_TEMPLATE_VIS move_iterator
7979
typedef typename iterator_traits<iterator_type>::difference_type difference_type;
8080
typedef iterator_type pointer;
8181

82-
#ifndef _LIBCPP_CXX03_LANG
8382
typedef typename iterator_traits<iterator_type>::reference __reference;
8483
typedef typename conditional<
8584
is_reference<__reference>::value,
8685
typename remove_reference<__reference>::type&&,
8786
__reference
8887
>::type reference;
89-
#else
90-
typedef typename iterator_traits<iterator_type>::reference reference;
91-
#endif
92-
9388
#endif // _LIBCPP_STD_VER > 17
9489

9590
#if _LIBCPP_STD_VER > 17

libcxx/include/__utility/move.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,10 @@ move(_Tp&& __t) _NOEXCEPT {
2626
return static_cast<_Up&&>(__t);
2727
}
2828

29-
#ifndef _LIBCPP_CXX03_LANG
3029
template <class _Tp>
3130
using __move_if_noexcept_result_t =
3231
typename conditional<!is_nothrow_move_constructible<_Tp>::value && is_copy_constructible<_Tp>::value, const _Tp&,
3332
_Tp&&>::type;
34-
#else // _LIBCPP_CXX03_LANG
35-
template <class _Tp>
36-
using __move_if_noexcept_result_t = const _Tp&;
37-
#endif
3833

3934
template <class _Tp>
4035
_LIBCPP_NODISCARD_EXT inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 __move_if_noexcept_result_t<_Tp>

libcxx/include/vector

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,15 @@ public:
434434
_LIBCPP_INLINE_VISIBILITY
435435
vector(initializer_list<value_type> __il, const allocator_type& __a);
436436

437+
_LIBCPP_INLINE_VISIBILITY
438+
vector& operator=(initializer_list<value_type> __il)
439+
{assign(__il.begin(), __il.end()); return *this;}
440+
#endif // !_LIBCPP_CXX03_LANG
441+
437442
_LIBCPP_INLINE_VISIBILITY
438443
vector(vector&& __x)
439444
#if _LIBCPP_STD_VER > 14
440-
_NOEXCEPT;
445+
noexcept;
441446
#else
442447
_NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
443448
#endif
@@ -448,12 +453,6 @@ public:
448453
vector& operator=(vector&& __x)
449454
_NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
450455

451-
_LIBCPP_INLINE_VISIBILITY
452-
vector& operator=(initializer_list<value_type> __il)
453-
{assign(__il.begin(), __il.end()); return *this;}
454-
455-
#endif // !_LIBCPP_CXX03_LANG
456-
457456
template <class _InputIterator>
458457
typename enable_if
459458
<
@@ -565,41 +564,26 @@ public:
565564
const value_type* data() const _NOEXCEPT
566565
{return _VSTD::__to_address(this->__begin_);}
567566

568-
#ifdef _LIBCPP_CXX03_LANG
569-
_LIBCPP_INLINE_VISIBILITY
570-
void __emplace_back(const value_type& __x) { push_back(__x); }
571-
#else
572-
template <class _Arg>
573-
_LIBCPP_INLINE_VISIBILITY
574-
void __emplace_back(_Arg&& __arg) {
575-
emplace_back(_VSTD::forward<_Arg>(__arg));
576-
}
577-
#endif
578-
579567
_LIBCPP_INLINE_VISIBILITY void push_back(const_reference __x);
580568

581-
#ifndef _LIBCPP_CXX03_LANG
582569
_LIBCPP_INLINE_VISIBILITY void push_back(value_type&& __x);
583570

584571
template <class... _Args>
585-
_LIBCPP_INLINE_VISIBILITY
572+
_LIBCPP_INLINE_VISIBILITY
586573
#if _LIBCPP_STD_VER > 14
587574
reference emplace_back(_Args&&... __args);
588575
#else
589576
void emplace_back(_Args&&... __args);
590577
#endif
591-
#endif // !_LIBCPP_CXX03_LANG
592578

593579
_LIBCPP_INLINE_VISIBILITY
594580
void pop_back();
595581

596582
iterator insert(const_iterator __position, const_reference __x);
597583

598-
#ifndef _LIBCPP_CXX03_LANG
599584
iterator insert(const_iterator __position, value_type&& __x);
600585
template <class... _Args>
601-
iterator emplace(const_iterator __position, _Args&&... __args);
602-
#endif // !_LIBCPP_CXX03_LANG
586+
iterator emplace(const_iterator __position, _Args&&... __args);
603587

604588
iterator insert(const_iterator __position, size_type __n, const_reference __x);
605589
template <class _InputIterator>
@@ -724,19 +708,13 @@ private:
724708
__annotate_shrink(__old_size);
725709
}
726710

727-
#ifndef _LIBCPP_CXX03_LANG
728711
template <class _Up>
729712
_LIBCPP_INLINE_VISIBILITY
730713
inline void __push_back_slow_path(_Up&& __x);
731714

732715
template <class... _Args>
733716
_LIBCPP_INLINE_VISIBILITY
734717
inline void __emplace_back_slow_path(_Args&&... __args);
735-
#else
736-
template <class _Up>
737-
_LIBCPP_INLINE_VISIBILITY
738-
inline void __push_back_slow_path(_Up& __x);
739-
#endif
740718

741719
// The following functions are no-ops outside of AddressSanitizer mode.
742720
// We call annotatations only for the default Allocator because other allocators
@@ -1110,7 +1088,7 @@ vector<_Tp, _Allocator>::vector(_InputIterator __first,
11101088
{
11111089
_VSTD::__debug_db_insert_c(this);
11121090
for (; __first != __last; ++__first)
1113-
__emplace_back(*__first);
1091+
emplace_back(*__first);
11141092
}
11151093

11161094
template <class _Tp, class _Allocator>
@@ -1125,7 +1103,7 @@ vector<_Tp, _Allocator>::vector(_InputIterator __first, _InputIterator __last, c
11251103
{
11261104
_VSTD::__debug_db_insert_c(this);
11271105
for (; __first != __last; ++__first)
1128-
__emplace_back(*__first);
1106+
emplace_back(*__first);
11291107
}
11301108

11311109
template <class _Tp, class _Allocator>
@@ -1190,13 +1168,11 @@ vector<_Tp, _Allocator>::vector(const vector& __x, const __type_identity_t<alloc
11901168
}
11911169
}
11921170

1193-
#ifndef _LIBCPP_CXX03_LANG
1194-
11951171
template <class _Tp, class _Allocator>
11961172
inline _LIBCPP_INLINE_VISIBILITY
11971173
vector<_Tp, _Allocator>::vector(vector&& __x)
11981174
#if _LIBCPP_STD_VER > 14
1199-
_NOEXCEPT
1175+
noexcept
12001176
#else
12011177
_NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
12021178
#endif
@@ -1231,6 +1207,8 @@ vector<_Tp, _Allocator>::vector(vector&& __x, const __type_identity_t<allocator_
12311207
}
12321208
}
12331209

1210+
#ifndef _LIBCPP_CXX03_LANG
1211+
12341212
template <class _Tp, class _Allocator>
12351213
inline _LIBCPP_INLINE_VISIBILITY
12361214
vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il)
@@ -1256,6 +1234,8 @@ vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocat
12561234
}
12571235
}
12581236

1237+
#endif // _LIBCPP_CXX03_LANG
1238+
12591239
template <class _Tp, class _Allocator>
12601240
inline _LIBCPP_INLINE_VISIBILITY
12611241
vector<_Tp, _Allocator>&
@@ -1295,8 +1275,6 @@ vector<_Tp, _Allocator>::__move_assign(vector& __c, true_type)
12951275
std::__debug_db_swap(this, std::addressof(__c));
12961276
}
12971277

1298-
#endif // !_LIBCPP_CXX03_LANG
1299-
13001278
template <class _Tp, class _Allocator>
13011279
inline _LIBCPP_INLINE_VISIBILITY
13021280
vector<_Tp, _Allocator>&
@@ -1325,7 +1303,7 @@ vector<_Tp, _Allocator>::assign(_InputIterator __first, _InputIterator __last)
13251303
{
13261304
clear();
13271305
for (; __first != __last; ++__first)
1328-
__emplace_back(*__first);
1306+
emplace_back(*__first);
13291307
}
13301308

13311309
template <class _Tp, class _Allocator>
@@ -1519,11 +1497,7 @@ vector<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
15191497
template <class _Tp, class _Allocator>
15201498
template <class _Up>
15211499
void
1522-
#ifndef _LIBCPP_CXX03_LANG
15231500
vector<_Tp, _Allocator>::__push_back_slow_path(_Up&& __x)
1524-
#else
1525-
vector<_Tp, _Allocator>::__push_back_slow_path(_Up& __x)
1526-
#endif
15271501
{
15281502
allocator_type& __a = this->__alloc();
15291503
__split_buffer<value_type, allocator_type&> __v(__recommend(size() + 1), size(), __a);
@@ -1546,8 +1520,6 @@ vector<_Tp, _Allocator>::push_back(const_reference __x)
15461520
__push_back_slow_path(__x);
15471521
}
15481522

1549-
#ifndef _LIBCPP_CXX03_LANG
1550-
15511523
template <class _Tp, class _Allocator>
15521524
inline _LIBCPP_INLINE_VISIBILITY
15531525
void
@@ -1595,8 +1567,6 @@ vector<_Tp, _Allocator>::emplace_back(_Args&&... __args)
15951567
#endif
15961568
}
15971569

1598-
#endif // !_LIBCPP_CXX03_LANG
1599-
16001570
template <class _Tp, class _Allocator>
16011571
inline
16021572
void
@@ -1693,8 +1663,6 @@ vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x)
16931663
return __make_iter(__p);
16941664
}
16951665

1696-
#ifndef _LIBCPP_CXX03_LANG
1697-
16981666
template <class _Tp, class _Allocator>
16991667
typename vector<_Tp, _Allocator>::iterator
17001668
vector<_Tp, _Allocator>::insert(const_iterator __position, value_type&& __x)
@@ -1755,8 +1723,6 @@ vector<_Tp, _Allocator>::emplace(const_iterator __position, _Args&&... __args)
17551723
return __make_iter(__p);
17561724
}
17571725

1758-
#endif // !_LIBCPP_CXX03_LANG
1759-
17601726
template <class _Tp, class _Allocator>
17611727
typename vector<_Tp, _Allocator>::iterator
17621728
vector<_Tp, _Allocator>::insert(const_iterator __position, size_type __n, const_reference __x)
@@ -2125,10 +2091,16 @@ public:
21252091
vector(initializer_list<value_type> __il);
21262092
vector(initializer_list<value_type> __il, const allocator_type& __a);
21272093

2094+
_LIBCPP_INLINE_VISIBILITY
2095+
vector& operator=(initializer_list<value_type> __il)
2096+
{assign(__il.begin(), __il.end()); return *this;}
2097+
2098+
#endif // !_LIBCPP_CXX03_LANG
2099+
21282100
_LIBCPP_INLINE_VISIBILITY
21292101
vector(vector&& __v)
21302102
#if _LIBCPP_STD_VER > 14
2131-
_NOEXCEPT;
2103+
noexcept;
21322104
#else
21332105
_NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
21342106
#endif
@@ -2137,12 +2109,6 @@ public:
21372109
vector& operator=(vector&& __v)
21382110
_NOEXCEPT_((__noexcept_move_assign_container<_Allocator, __alloc_traits>::value));
21392111

2140-
_LIBCPP_INLINE_VISIBILITY
2141-
vector& operator=(initializer_list<value_type> __il)
2142-
{assign(__il.begin(), __il.end()); return *this;}
2143-
2144-
#endif // !_LIBCPP_CXX03_LANG
2145-
21462112
template <class _InputIterator>
21472113
typename enable_if
21482114
<
@@ -2739,8 +2705,6 @@ vector<bool, _Allocator>::operator=(const vector& __v)
27392705
return *this;
27402706
}
27412707

2742-
#ifndef _LIBCPP_CXX03_LANG
2743-
27442708
template <class _Allocator>
27452709
inline _LIBCPP_INLINE_VISIBILITY vector<bool, _Allocator>::vector(vector&& __v)
27462710
#if _LIBCPP_STD_VER > 14
@@ -2812,8 +2776,6 @@ vector<bool, _Allocator>::__move_assign(vector& __c, true_type)
28122776
__c.__cap() = __c.__size_ = 0;
28132777
}
28142778

2815-
#endif // !_LIBCPP_CXX03_LANG
2816-
28172779
template <class _Allocator>
28182780
void
28192781
vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)

libcxx/test/std/containers/sequences/array/array.creation/to_array.fail.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ int main(int, char**) {
2828
{
2929
MoveOnly mo[] = {MoveOnly{3}};
3030
// expected-error@array:* {{to_array requires copy constructible elements}}
31-
// expected-error@array:* {{call to implicitly-deleted copy constructor of 'MoveOnly'}}
31+
// expected-error-re@array:* {{{{(call to implicitly-deleted copy constructor of 'MoveOnly')|(call to deleted constructor of 'MoveOnly')}}}}
3232
std::to_array(mo); // expected-note {{requested here}}
3333
}
3434

3535
{
3636
const MoveOnly cmo[] = {MoveOnly{3}};
3737
// expected-error@array:* {{to_array requires move constructible elements}}
38-
// expected-error@array:* {{call to implicitly-deleted copy constructor of 'MoveOnly'}}
38+
// expected-error-re@array:* {{{{(call to implicitly-deleted copy constructor of 'MoveOnly')|(call to deleted constructor of 'MoveOnly')}}}}
3939
std::to_array(std::move(cmo)); // expected-note {{requested here}}
4040
}
4141

libcxx/test/std/containers/sequences/vector/vector.cons/assign_move.pass.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: c++03
9+
// UNSUPPORTED: c++03 && !stdlib=libc++
1010

1111
// <vector>
1212

@@ -78,8 +78,8 @@ int main(int, char**)
7878
assert(is_contiguous_container_asan_correct(l2));
7979
}
8080
{
81-
std::vector<MoveOnly, min_allocator<MoveOnly> > l(min_allocator<MoveOnly>{});
82-
std::vector<MoveOnly, min_allocator<MoveOnly> > lo(min_allocator<MoveOnly>{});
81+
std::vector<MoveOnly, min_allocator<MoveOnly> > l((min_allocator<MoveOnly>()));
82+
std::vector<MoveOnly, min_allocator<MoveOnly> > lo((min_allocator<MoveOnly>()));
8383
assert(is_contiguous_container_asan_correct(l));
8484
assert(is_contiguous_container_asan_correct(lo));
8585
for (int i = 1; i <= 3; ++i)
@@ -89,7 +89,7 @@ int main(int, char**)
8989
}
9090
assert(is_contiguous_container_asan_correct(l));
9191
assert(is_contiguous_container_asan_correct(lo));
92-
std::vector<MoveOnly, min_allocator<MoveOnly> > l2(min_allocator<MoveOnly>{});
92+
std::vector<MoveOnly, min_allocator<MoveOnly> > l2((min_allocator<MoveOnly>()));
9393
l2 = std::move(l);
9494
assert(l2 == lo);
9595
assert(l.empty());

libcxx/test/std/containers/sequences/vector/vector.cons/copy.move_only.verify.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,15 @@
88

99
// Make sure that a std::vector containing move-only types can't be copied.
1010

11-
// UNSUPPORTED: c++03
11+
// UNSUPPORTED: c++03 && !stdlib=libc++
1212

1313
#include <vector>
1414

15-
struct move_only
16-
{
17-
move_only() = default;
18-
move_only(move_only&&) = default;
19-
move_only& operator=(move_only&&) = default;
20-
};
15+
#include "MoveOnly.h"
2116

2217
int main(int, char**)
2318
{
24-
std::vector<move_only> v;
25-
std::vector<move_only> copy = v; // expected-error-re@* {{{{(no matching function for call to 'construct_at')|(call to implicitly-deleted copy constructor of 'move_only')}}}}
19+
std::vector<MoveOnly> v;
20+
std::vector<MoveOnly> copy = v; // expected-error-re@* {{{{(no matching function for call to 'construct_at')|(call to implicitly-deleted copy constructor of 'MoveOnly')|(call to deleted constructor of 'MoveOnly')}}}}
2621
return 0;
2722
}

libcxx/test/std/containers/sequences/vector/vector.cons/move.addressof.compile.pass.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: c++03
9+
// UNSUPPORTED: c++03 && !stdlib=libc++
1010

1111
// <vector>
1212

@@ -21,11 +21,11 @@
2121

2222
void test() {
2323
{
24-
std::vector<operator_hijacker> vo{};
25-
std::vector<operator_hijacker> v{std::move(vo)};
24+
std::vector<operator_hijacker> vo;
25+
std::vector<operator_hijacker> v(std::move(vo));
2626
}
2727
{
28-
std::vector<operator_hijacker> vo{};
29-
std::vector<operator_hijacker> v{std::move(vo), std::allocator<operator_hijacker>{}};
28+
std::vector<operator_hijacker> vo;
29+
std::vector<operator_hijacker> v(std::move(vo), std::allocator<operator_hijacker>());
3030
}
3131
}

0 commit comments

Comments
 (0)