| 1 | #include <iostream>
|
|---|
| 2 | #include <boost/noncopyable.hpp>
|
|---|
| 3 | #include <boost/intrusive/any_hook.hpp>
|
|---|
| 4 | #include <boost/intrusive/set.hpp>
|
|---|
| 5 |
|
|---|
| 6 | using namespace boost;
|
|---|
| 7 | using namespace boost::intrusive;
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 | template <class Hook, class Option>
|
|---|
| 11 | struct test {
|
|---|
| 12 |
|
|---|
| 13 | class item
|
|---|
| 14 | : public noncopyable
|
|---|
| 15 | , public Hook
|
|---|
| 16 | {
|
|---|
| 17 | public:
|
|---|
| 18 | item( int k )
|
|---|
| 19 | :k(k)
|
|---|
| 20 | {
|
|---|
| 21 | }
|
|---|
| 22 | const int k;
|
|---|
| 23 | bool operator<( const item& other ) const {
|
|---|
| 24 | return k < other.k;
|
|---|
| 25 | }
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | typedef boost::intrusive::set< item, Option > set;
|
|---|
| 29 |
|
|---|
| 30 | static void go() {
|
|---|
| 31 |
|
|---|
| 32 | set s;
|
|---|
| 33 | item i1(1);
|
|---|
| 34 | item i2(2);
|
|---|
| 35 | item i3(3);
|
|---|
| 36 |
|
|---|
| 37 | assert( s.insert(i1).second );
|
|---|
| 38 | assert( s.insert(i2).second );
|
|---|
| 39 | assert( s.insert(i3).second );
|
|---|
| 40 |
|
|---|
| 41 | for ( auto& i : s )
|
|---|
| 42 | std::cout << i.k << " ";
|
|---|
| 43 | std::cout << std::endl;
|
|---|
| 44 |
|
|---|
| 45 | s.clear();
|
|---|
| 46 | }
|
|---|
| 47 | };
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 | int main(int argc, char* argv[]) {
|
|---|
| 53 |
|
|---|
| 54 | typedef any_base_hook<> A;
|
|---|
| 55 | typedef set_base_hook<> S;
|
|---|
| 56 | typedef base_hook<S> SO;
|
|---|
| 57 | typedef test< S, SO > without_any;
|
|---|
| 58 | typedef test< A, any_to_set_hook<base_hook<A>> > with_any;
|
|---|
| 59 |
|
|---|
| 60 | // working: prints 1 2 3
|
|---|
| 61 | without_any::go();
|
|---|
| 62 |
|
|---|
| 63 | // not working: prints 1 2 3 2 3 2 3 2 3... (infinite loop)
|
|---|
| 64 | with_any::go();
|
|---|
| 65 |
|
|---|
| 66 | return 0;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|