Ticket #10322: bug-erase.cpp

File bug-erase.cpp, 1.1 KB (added by Matei David <matei@…>, 8 years ago)

demonstrates the bug (compile with std=c++11)

Line 
1#include <iostream>
2#include <boost/intrusive/set.hpp>
3
4namespace bi = boost::intrusive;
5
6struct A
7 : public bi::set_base_hook<>
8{
9 int _val;
10 int _extra;
11
12 A(int val = 0, int extra = 0) : _val(val), _extra(extra) {}
13
14 friend std::ostream& operator << (std::ostream& os, const A& rhs)
15 {
16 os << "(val=" << rhs._val << ", extra=" << rhs._extra << ")";
17 return os;
18 }
19
20 friend bool operator < (const A& lhs, const A& rhs)
21 {
22 return lhs._val < rhs._val;
23 }
24};
25
26template < typename Cont >
27std::ostream& print_cont(std::ostream& os, const Cont& l)
28{
29 bool first = true;
30 for (const auto& e : l)
31 {
32 if (not first)
33 {
34 os << ", ";
35 }
36 os << e;
37 first = false;
38 }
39 return os;
40}
41
42int main()
43{
44 std::vector< A > v = { {2, 5}, {2, 7} };
45 bi::multiset< A > s;
46 s.insert(v[0]);
47 s.insert(v[1]);
48
49 std::cout << "s: size=" << s.size() << ", elems={";
50 print_cont(std::cout, s);
51 std::cout << "}\n";
52
53 s.erase(v[0]);
54
55 std::cout << "s: size=" << s.size() << ", elems={";
56 print_cont(std::cout, s);
57 std::cout << "}\n";
58}