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// <unordered_set>
10
11// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
12// class Alloc = allocator<Value>>
13// class unordered_set
14
15// iterator erase(const_iterator p)
16
17#include <unordered_set>
18#include <cassert>
19
20#include "test_macros.h"
21#include "min_allocator.h"
22
23struct TemplateConstructor {
24 template <typename T>
25 TemplateConstructor(const T&) {}
26};
27
28bool operator==(const TemplateConstructor&, const TemplateConstructor&) { return false; }
29struct Hash {
30 std::size_t operator()(const TemplateConstructor&) const { return 0; }
31};
32
33int main(int, char**) {
34 {
35 typedef std::unordered_set<int> C;
36 typedef int P;
37 P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)};
38 C c(a, a + sizeof(a) / sizeof(a[0]));
39 C::const_iterator i = c.find(x: 2);
40 C::const_iterator i_next = i;
41 ++i_next;
42 C::iterator j = c.erase(position: i);
43 assert(j == i_next);
44
45 assert(c.size() == 3);
46 assert(c.count(1) == 1);
47 assert(c.count(3) == 1);
48 assert(c.count(4) == 1);
49 }
50#if TEST_STD_VER >= 11
51 {
52 typedef std::unordered_set<int, std::hash<int>, std::equal_to<int>, min_allocator<int>> C;
53 typedef int P;
54 P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)};
55 C c(a, a + sizeof(a) / sizeof(a[0]));
56 C::const_iterator i = c.find(2);
57 C::const_iterator i_next = i;
58 ++i_next;
59 C::iterator j = c.erase(i);
60 assert(j == i_next);
61
62 assert(c.size() == 3);
63 assert(c.count(1) == 1);
64 assert(c.count(3) == 1);
65 assert(c.count(4) == 1);
66 }
67#endif
68#if TEST_STD_VER >= 14
69 {
70 // This is LWG #2059
71 typedef TemplateConstructor T;
72 typedef std::unordered_set<T, Hash> C;
73 typedef C::iterator I;
74
75 C m;
76 T a{0};
77 I it = m.find(a);
78 if (it != m.end())
79 m.erase(it);
80 }
81#endif
82
83 return 0;
84}
85

source code of libcxx/test/std/containers/unord/unord.set/erase_const_iter.pass.cpp