Skip to content

Commit 4632706

Browse files
committed
[libc++] fix flat_{multi}set insert_range
1 parent d591630 commit 4632706

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

libcxx/include/__flat_set/utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define _LIBCPP___FLAT_SET_UTILS_H
1212

1313
#include <__config>
14+
#include <__iterator/iterator_traits.h>
1415
#include <__ranges/access.h>
1516
#include <__ranges/concepts.h>
1617
#include <__type_traits/container_traits.h>
@@ -60,7 +61,8 @@ struct __flat_set_utils {
6061
// C++23 Sequence Container should have insert_range member function
6162
// Note that not all Sequence Containers provide append_range.
6263
__set.__keys_.insert_range(__set.__keys_.end(), std::forward<_Range>(__rng));
63-
} else if constexpr (ranges::common_range<_Range>) {
64+
} else if constexpr (ranges::common_range<_Range> &&
65+
__has_input_iterator_category<ranges::iterator_t<_Range>>::value) {
6466
__set.__keys_.insert(__set.__keys_.end(), ranges::begin(__rng), ranges::end(__rng));
6567
} else {
6668
for (auto&& __x : __rng) {

libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.modifiers/insert_range.pass.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <flat_set>
1919
#include <functional>
2020
#include <ranges>
21+
#include <sstream>
2122
#include <vector>
2223

2324
#include "MinSequenceContainer.h"
@@ -85,6 +86,15 @@ void test() {
8586
MoveOnly expected[] = {1, 1, 3, 4, 5};
8687
assert(std::ranges::equal(m, expected));
8788
}
89+
{
90+
// https://github.com/llvm/llvm-project/issues/136656
91+
MinSequenceContainer<int> v;
92+
std::flat_multiset s(v);
93+
std::istringstream ints("0 1 1 0");
94+
auto r = std::ranges::subrange(std::istream_iterator<int>(ints), std::istream_iterator<int>()) |
95+
std::views::transform([](int i) { return i * i; });
96+
s.insert_range(r);
97+
}
8898
}
8999

90100
void test_exception() {

libcxx/test/std/containers/container.adaptors/flat.set/flat.set.modifiers/insert_range.pass.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <flat_set>
1919
#include <functional>
2020
#include <ranges>
21+
#include <sstream>
2122
#include <vector>
2223

2324
#include "MinSequenceContainer.h"
@@ -96,6 +97,15 @@ void test() {
9697
MoveOnly expected[] = {1, 3, 4, 5};
9798
assert(std::ranges::equal(m, expected));
9899
}
100+
{
101+
// https://github.com/llvm/llvm-project/issues/136656
102+
MinSequenceContainer<int> v;
103+
std::flat_set s(v);
104+
std::istringstream ints("0 1 1 0");
105+
auto r = std::ranges::subrange(std::istream_iterator<int>(ints), std::istream_iterator<int>()) |
106+
std::views::transform([](int i) { return i * i; });
107+
s.insert_range(r);
108+
}
99109
}
100110

101111
void test_exception() {

0 commit comments

Comments
 (0)