Skip to content

Commit e751289

Browse files
add constexpr for insert_node_type and insert_node_type_hint
1 parent adc2589 commit e751289

File tree

3 files changed

+40
-31
lines changed

3 files changed

+40
-31
lines changed

libcxx/include/__tree

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,9 +2132,10 @@ __tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(_NodeHandle&& __n
21322132
__node_pointer __ptr = __nh.__ptr_;
21332133
auto [__parent, __child] = __find_equal(__ptr->__get_value());
21342134
if (__child != nullptr)
2135-
return _InsertReturnType{iterator(static_cast<__node_pointer>(__child)), false, std::move(__nh)};
2135+
return _InsertReturnType{
2136+
iterator(std::__static_fancy_pointer_cast<__node_pointer>(__child)), false, std::move(__nh)};
21362137

2137-
__insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2138+
__insert_node_at(__parent, __child, std::__static_fancy_pointer_cast<__node_base_pointer>(__ptr));
21382139
__nh.__release_ptr();
21392140
return _InsertReturnType{iterator(__ptr), true, _NodeHandle()};
21402141
}
@@ -2151,7 +2152,7 @@ __tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(const_iterator __
21512152
auto [__parent, __child] = __find_equal(__hint, __dummy, __ptr->__get_value());
21522153
__node_pointer __r = static_cast<__node_pointer>(__child);
21532154
if (__child == nullptr) {
2154-
__insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2155+
__insert_node_at(__parent, __child, std::__static_fancy_pointer_cast<__node_base_pointer>(__ptr));
21552156
__r = __ptr;
21562157
__nh.__release_ptr();
21572158
}

libcxx/test/std/containers/associative/map/map.modifiers/insert_node_type.pass.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
// class map
1414

15-
// insert_return_type insert(node_type&&);
15+
// constexpr insert_return_type insert(node_type&&); // constexpr since C++26
1616

1717
#include <map>
1818
#include <memory>
@@ -21,7 +21,7 @@
2121
#include "min_allocator.h"
2222

2323
template <class Container, class T>
24-
void verify_insert_return_type(T&& t) {
24+
TEST_CONSTEXPR_CXX26 void verify_insert_return_type(T&& t) {
2525
using verified_type = std::remove_cv_t<std::remove_reference_t<T>>;
2626
static_assert(std::is_aggregate_v<verified_type>);
2727
static_assert(std::is_same_v<verified_type, typename Container::insert_return_type>);
@@ -42,20 +42,20 @@ void verify_insert_return_type(T&& t) {
4242
}
4343

4444
template <class Container>
45-
typename Container::node_type
45+
TEST_CONSTEXPR_CXX26 std::pair<Container, typename Container::node_type>
4646
node_factory(typename Container::key_type const& key, typename Container::mapped_type const& mapped) {
47-
static Container c;
47+
Container c;
4848
auto p = c.insert({key, mapped});
4949
assert(p.second);
50-
return c.extract(p.first);
50+
return {c, c.extract(p.first)};
5151
}
5252

5353
template <class Container>
54-
TEST_CONSTEXPR_CXX26 bool test(Container& c) {
54+
TEST_CONSTEXPR_CXX26 void testContainer(Container& c) {
5555
auto* nf = &node_factory<Container>;
5656

5757
for (int i = 0; i != 10; ++i) {
58-
typename Container::node_type node = nf(i, i + 1);
58+
auto [/*Container*/ staticContainer, /*typename Container::node_type*/ node] = nf(i, i + 1);
5959
assert(!node.empty());
6060
typename Container::insert_return_type irt = c.insert(std::move(node));
6161
assert(node.empty());
@@ -78,13 +78,14 @@ TEST_CONSTEXPR_CXX26 bool test(Container& c) {
7878
}
7979

8080
{ // Insert duplicate node.
81-
typename Container::node_type dupl = nf(0, 42);
81+
auto [/*Container*/ staticContainer, /*typename Container::node_type*/ dupl] = nf(0, 42);
8282
auto irt = c.insert(std::move(dupl));
8383
assert(dupl.empty());
8484
assert(!irt.inserted);
8585
assert(!irt.node.empty());
8686
assert(irt.position == c.find(0));
87-
assert(irt.node.key() == 0 && irt.node.mapped() == 42);
87+
if (!TEST_IS_CONSTANT_EVALUATED)
88+
assert(irt.node.key() == 0 && irt.node.mapped() == 42);
8889
verify_insert_return_type<Container>(irt);
8990
}
9091

@@ -94,24 +95,21 @@ TEST_CONSTEXPR_CXX26 bool test(Container& c) {
9495
assert(c.count(i) == 1);
9596
assert(c[i] == i + 1);
9697
}
98+
}
99+
100+
TEST_CONSTEXPR_CXX26
101+
bool test() {
102+
std::map<int, int> m;
103+
testContainer(m);
104+
std::map<int, int, std::less<int>, min_allocator<std::pair<const int, int>>> m2;
105+
testContainer(m2);
97106
return true;
98107
}
99108

100109
int main(int, char**) {
101-
{
102-
std::map<int, int> m;
103-
test(m);
104-
std::map<int, int, std::less<int>, min_allocator<std::pair<const int, int>>> m2;
105-
test(m2);
106-
}
107-
110+
test();
108111
#if TEST_STD_VER >= 26
109-
{
110-
std::map<int, int> m;
111-
test(m);
112-
std::map<int, int, std::less<int>, min_allocator<std::pair<const int, int>>> m2;
113-
test(m2);
114-
}
112+
static_assert(test());
115113
#endif
116114

117115
return 0;

libcxx/test/std/containers/associative/map/map.modifiers/insert_node_type_hint.pass.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@
1919
#include "min_allocator.h"
2020

2121
template <class Container>
22-
typename Container::node_type
22+
TEST_CONSTEXPR_CXX26 std::pair<Container, typename Container::node_type>
2323
node_factory(typename Container::key_type const& key, typename Container::mapped_type const& mapped) {
24-
static Container c;
24+
Container c;
2525
auto p = c.insert({key, mapped});
2626
assert(p.second);
27-
return c.extract(p.first);
27+
return {c, c.extract(p.first)};
2828
}
2929

3030
template <class Container>
31-
void test(Container& c) {
31+
TEST_CONSTEXPR_CXX26 void test(Container& c) {
3232
auto* nf = &node_factory<Container>;
3333

3434
for (int i = 0; i != 10; ++i) {
35-
typename Container::node_type node = nf(i, i + 1);
35+
auto [/*Container*/ staticContainer, /*typename Container::node_type*/ node] = nf(i, i + 1);
3636
assert(!node.empty());
3737
std::size_t prev = c.size();
3838
auto it = c.insert(c.end(), std::move(node));
@@ -50,11 +50,21 @@ void test(Container& c) {
5050
}
5151
}
5252

53-
int main(int, char**) {
53+
TEST_CONSTEXPR_CXX26
54+
bool test() {
5455
std::map<int, int> m;
5556
test(m);
5657
std::map<int, int, std::less<int>, min_allocator<std::pair<const int, int>>> m2;
5758
test(m2);
59+
return true;
60+
}
61+
62+
int main(int, char**) {
63+
test();
64+
65+
#if TEST_STD_VER >= 26
66+
static_assert(test());
67+
#endif
5868

5969
return 0;
6070
}

0 commit comments

Comments
 (0)