aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Palka <ppalka@redhat.com>2025-01-31 15:53:10 -0500
committerPatrick Palka <ppalka@redhat.com>2025-01-31 15:53:10 -0500
commitee797739606ce9b8cf6ebb0236977861e49aa0d1 (patch)
tree4f5d58df579dea134fcebd3b55a2834d73147b8b
parentd6418fe22684f9335474d1fd405ade45954c069d (diff)
downloadgcc-ee797739606ce9b8cf6ebb0236977861e49aa0d1.zip
gcc-ee797739606ce9b8cf6ebb0236977861e49aa0d1.tar.gz
gcc-ee797739606ce9b8cf6ebb0236977861e49aa0d1.tar.bz2
libstdc++: Fix return value of vector::insert_range
In some cases we're wrongly returning an iterator to (one past) the last element inserted instead of to the first element inserted. libstdc++-v3/ChangeLog: * include/bits/stl_bvector.h (vector<bool>::insert_range): Consistently return an iterator pointing to the first element inserted. * include/bits/vector.tcc (vector::insert_range): Likewise. * testsuite/23_containers/vector/bool/modifiers/insert/insert_range.cc: Verify insert_range return values. * testsuite/23_containers/vector/modifiers/insert/insert_range.cc: Likewise. Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
-rw-r--r--libstdc++-v3/include/bits/stl_bvector.h8
-rw-r--r--libstdc++-v3/include/bits/vector.tcc3
-rw-r--r--libstdc++-v3/testsuite/23_containers/vector/bool/modifiers/insert/insert_range.cc18
-rw-r--r--libstdc++-v3/testsuite/23_containers/vector/modifiers/insert/insert_range.cc18
4 files changed, 30 insertions, 17 deletions
diff --git a/libstdc++-v3/include/bits/stl_bvector.h b/libstdc++-v3/include/bits/stl_bvector.h
index 961e4a2..2292eec 100644
--- a/libstdc++-v3/include/bits/stl_bvector.h
+++ b/libstdc++-v3/include/bits/stl_bvector.h
@@ -1341,9 +1341,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
std::copy_backward(__pos._M_const_cast(), end(),
this->_M_impl._M_finish
+ difference_type(__n));
- auto __i = ranges::copy(__rg, __pos._M_const_cast()).out;
+ ranges::copy(__rg, __pos._M_const_cast());
this->_M_impl._M_finish += difference_type(__n);
- return __i;
+ return __pos._M_const_cast();
}
else
{
@@ -1355,9 +1355,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
iterator __i = _M_copy_aligned(__begin,
__pos._M_const_cast(),
__start);
- __i = ranges::copy(__rg, __i).out;
+ iterator __j = ranges::copy(__rg, __i).out;
iterator __finish = std::copy(__pos._M_const_cast(),
- __end, __i);
+ __end, __j);
this->_M_deallocate();
this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
this->_M_impl._M_start = __start;
diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc
index 4f4c366..acb2f5f 100644
--- a/libstdc++-v3/include/bits/vector.tcc
+++ b/libstdc++-v3/include/bits/vector.tcc
@@ -984,8 +984,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
{
if (__pos == cend())
{
+ const auto __ins_idx = size();
append_range(std::forward<_Rg>(__rg));
- return end();
+ return begin() + __ins_idx;
}
if constexpr (ranges::forward_range<_Rg>)
diff --git a/libstdc++-v3/testsuite/23_containers/vector/bool/modifiers/insert/insert_range.cc b/libstdc++-v3/testsuite/23_containers/vector/bool/modifiers/insert/insert_range.cc
index 4f48357..5c65610 100644
--- a/libstdc++-v3/testsuite/23_containers/vector/bool/modifiers/insert/insert_range.cc
+++ b/libstdc++-v3/testsuite/23_containers/vector/bool/modifiers/insert/insert_range.cc
@@ -35,16 +35,22 @@ do_test()
VERIFY( eq(v, a) );
v.clear();
v.shrink_to_fit();
- v.insert_range(v.begin(), Range(a, a+3));
- v.insert_range(v.end(), Range(a+6, a+9));
- v.insert_range(v.begin()+3, Range(a+3, a+6));
+ auto it = v.insert_range(v.begin(), Range(a, a+3));
+ VERIFY( it == v.begin() );
+ it = v.insert_range(v.end(), Range(a+6, a+9));
+ VERIFY( it == v.begin()+3 );
+ it = v.insert_range(v.begin()+3, Range(a+3, a+6));
+ VERIFY( it == v.begin()+3 );
VERIFY( eq(v, a) );
v.resize(3);
- v.insert_range(v.begin()+1, Range(a+4, a+9));
- v.insert_range(v.begin()+1, Range(a+1, a+4));
+ it = v.insert_range(v.begin()+1, Range(a+4, a+9));
+ VERIFY( it == v.begin()+1 );
+ it = v.insert_range(v.begin()+1, Range(a+1, a+4));
+ VERIFY( it == v.begin()+1 );
v.resize(9);
VERIFY( eq(v, a) );
- v.insert_range(v.begin(), Range(a, a));
+ it = v.insert_range(v.begin(), Range(a, a));
+ VERIFY( it == v.begin() );
VERIFY( eq(v, a) );
}
diff --git a/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert/insert_range.cc b/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert/insert_range.cc
index 68218e9..5907143 100644
--- a/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert/insert_range.cc
+++ b/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert/insert_range.cc
@@ -39,16 +39,22 @@ do_test()
VERIFY( eq(v, a) );
v.clear();
v.shrink_to_fit();
- v.insert_range(v.begin(), Range(a, a+3));
- v.insert_range(v.end(), Range(a+6, a+9));
- v.insert_range(v.begin()+3, Range(a+3, a+6));
+ auto it = v.insert_range(v.begin(), Range(a, a+3));
+ VERIFY( it == v.begin() );
+ it = v.insert_range(v.end(), Range(a+6, a+9));
+ VERIFY( it == v.begin()+3 );
+ it = v.insert_range(v.begin()+3, Range(a+3, a+6));
+ VERIFY( it == v.begin()+3 );
VERIFY( eq(v, a) );
v.resize(3);
- v.insert_range(v.begin()+1, Range(a+4, a+9));
- v.insert_range(v.begin()+1, Range(a+1, a+4));
+ it = v.insert_range(v.begin()+1, Range(a+4, a+9));
+ VERIFY( it == v.begin()+1 );
+ it = v.insert_range(v.begin()+1, Range(a+1, a+4));
+ VERIFY( it == v.begin()+1 );
v.resize(9);
VERIFY( eq(v, a) );
- v.insert_range(v.begin() + 6, Range(a, a));
+ it = v.insert_range(v.begin() + 6, Range(a, a));
+ VERIFY( it == v.begin() + 6 );
VERIFY( eq(v, a) );
}