Skip to content

Commit df4c5a8

Browse files
committed
[libc++] Always initialize __tree::{,const_}iterator
1 parent d3b339e commit df4c5a8

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

libcxx/include/__tree

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -646,12 +646,7 @@ public:
646646
using reference = value_type&;
647647
using pointer = __rebind_pointer_t<_NodePtr, value_type>;
648648

649-
_LIBCPP_HIDE_FROM_ABI __tree_iterator() _NOEXCEPT
650-
#if _LIBCPP_STD_VER >= 14
651-
: __ptr_(nullptr)
652-
#endif
653-
{
654-
}
649+
_LIBCPP_HIDE_FROM_ABI __tree_iterator() _NOEXCEPT : __ptr_(nullptr) {}
655650

656651
_LIBCPP_HIDE_FROM_ABI reference operator*() const { return __get_np()->__value_; }
657652
_LIBCPP_HIDE_FROM_ABI pointer operator->() const { return pointer_traits<pointer>::pointer_to(__get_np()->__value_); }
@@ -720,12 +715,7 @@ public:
720715
using reference = const value_type&;
721716
using pointer = __rebind_pointer_t<_NodePtr, const value_type>;
722717

723-
_LIBCPP_HIDE_FROM_ABI __tree_const_iterator() _NOEXCEPT
724-
#if _LIBCPP_STD_VER >= 14
725-
: __ptr_(nullptr)
726-
#endif
727-
{
728-
}
718+
_LIBCPP_HIDE_FROM_ABI __tree_const_iterator() _NOEXCEPT : __ptr_(nullptr) {}
729719

730720
private:
731721
typedef __tree_iterator<_Tp, __node_pointer, difference_type> __non_const_iterator;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03
10+
11+
// Make sure that that the iterator types of the associative containers is initialized even when default initialized.
12+
// This is an extension because the standard only requires initialization when an iterator is value initialized, and
13+
// that only since C++14. We guarantee that iterators are always initialized, even in C++11.
14+
15+
#include <cassert>
16+
#include <map>
17+
#include <set>
18+
19+
template <class Iter>
20+
void test() {
21+
{
22+
Iter iter1;
23+
Iter iter2;
24+
assert(iter1 == iter2);
25+
}
26+
{
27+
Iter iter1;
28+
Iter iter2{};
29+
assert(iter1 == iter2);
30+
}
31+
{
32+
Iter iter1{};
33+
Iter iter2;
34+
assert(iter1 == iter2);
35+
}
36+
}
37+
38+
template <class Container>
39+
void test_container() {
40+
test<typename Container::iterator>();
41+
test<typename Container::const_iterator>();
42+
test<typename Container::reverse_iterator>();
43+
test<typename Container::const_reverse_iterator>();
44+
}
45+
46+
int main(int, char**) {
47+
test_container<std::map<int, int>>();
48+
test_container<std::multimap<int, int>>();
49+
test_container<std::set<int>>();
50+
test_container<std::multiset<int>>();
51+
52+
return 0;
53+
}

0 commit comments

Comments
 (0)