This repository was archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart.cpp
More file actions
62 lines (46 loc) ยท 1.45 KB
/
smart.cpp
File metadata and controls
62 lines (46 loc) ยท 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#if 0
#include <iostream>
#include <memory>
#include <list>
#include <algorithm>
using namespace std;
struct Foo
{
Foo(int number)
: number(number)
{ cout << "ctor(" << number << ")"<< endl; }
~Foo()
{ cout << "dtor(" << number << ")"<< endl; }
int number;
};
void main()
{
list<shared_ptr<Foo>> originallist;;
list<weak_ptr<Foo>> list;
//์๋ณธ์ด ์กด์ฌํ์ง ์๋ weak_ptr์ dangle์ด๋ฏ๋ก ์๋ณธ์ ์ ์งํ๋ ๋ฆฌ์คํธ๋ฅผ ์์ฑ.
for (int i = 0; i < 10; i++)
originallist.push_back( make_shared<Foo>(i) );
for (auto& itr : originallist)
list.push_back( itr );
weak_ptr<Foo> temp;
const int someIndex = 6;
for (auto itr : list)
{
//์ด ๋ฃจํ ๋ด์์๋ง shaerd_ptr๋ก ์ฌ์ฉ.
//๋ฐฉ์ด์ ์ฝ๋ฉ์ ํ ๋๋ lock์ ๊ฒฐ๊ณผ๊ฐ nullptr์ด ์๋ ๊ฒ์ ํ์ธํ ํ์ ์ฌ์ฉํด์ผ ์์ ํ๋ค.
auto item (itr.lock());
cout << item->number << endl;
if (item->number == someIndex)
temp = itr;
}
//weak_ptr์ operator== ์ ๊ตฌํํ์ง ์๋๋ค. ๋ฐ๋ผ์ find๋ฅผ ์ฌ์ฉํ ์ ์์
//find๊ฐ ํ์ํ ๊ฒฝ์ฐ find_if์ shared_ptr์ ๋น๊ตํ๋ lambda๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
//D๋ผ๋ฉด lambda์์๋ ํ์
์ถ๋ก ์ด ๋๋๊น auto ๋ฃ์ผ๋ฉด ๋์ธ๋ฐ
//C++์ lambda์์์๋ auto ์ฌ์ฉ ๋ถ๊ฐ๋ฅ์ด๋ผ ์ ๊ฐ.
auto selector = [&temp](weak_ptr<Foo> item){ return item.lock() == temp.lock();};
auto itr (std::find_if(list.begin(), list.end(), selector));
auto foundItem (itr->lock());
cout << "found item : " << foundItem->number << endl;
auto t = (((unsigned long)(1)<<31) | ((unsigned long)(0x876)<<16) | ((unsigned long)(0))) ;
}
#endif