Your SlideShare is downloading. ×
0
Episteme unique_ptr
Episteme unique_ptr
Episteme unique_ptr
Episteme unique_ptr
Episteme unique_ptr
Episteme unique_ptr
Episteme unique_ptr
Episteme unique_ptr
Episteme unique_ptr
Episteme unique_ptr
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Episteme unique_ptr

299

Published on

- 2013.11.02 C++例外安全Day@大阪 …

- 2013.11.02 C++例外安全Day@大阪
- 2013.11.09 わんくま同盟東京勉強会#85
- 2013.11.14 歌舞伎座.tech #2

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
299
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. わんくま同盟茶藝部顧問 episthmh episteme@cppll.jp Microsoft MVP for Visual C++ (2004~)
  • 2. 本日、CodeZine に載りました♪
  • 3. auto_ptr は deprecated  make_unique (C++14) が 一足お先に Visual Studio 2013 に 
  • 4. { unique_ptr<foo> pfoo(new foo(“adam”,10)); foo* p = pfoo.get(); pfoo->mem_fun(); (*pfoo).mem_fun(); … // デストラクタが”内包するポインタ”をdeleteする }
  • 5. { unique_ptr<foo[]> afoo(new foo[4]); for ( int i = 0; i < 4; ++i ) { afoo[i].mem_fun(); } // デストラクタが”内包するポインタ”を delete[]する }
  • 6. { auto pfoo = make_unique<foo>(“adam”,10); auto afoo = make_unique<foo[]>(4); … }
  • 7. unique_ptr<foo> p; unique_ptr<foo> q; p = q; // error!(見た目はcopy/挙動はmove) p = std::move(q); // OK ※ unique_ptr& operator=(const unique_ptr&) = delete; そのかわり unique_ptr& operator=(unique_ptr&&); // 右辺値参照!
  • 8. namespace std { template<class T, class D=default_delete<T>> class unique_ptr; template<class T> struct default_delete { // ポインタを手放す際にコレ↓を発動する void operator()(T* p) const { delete p; } } }
  • 9. template<> struct std::default_delete<FILE> { void operator()(FILE* p) const { fclose(p); } }; { auto fp = unique_ptr<FILE>(fopen(“foo.txt”,”w”)); fprintf(fp.get(), “Hello, worldn”); // 自動的に fclose! }
  • 10. { typedef unique_ptr<FILE,function<void(FILE*)>> unique_file; auto fp = unique_file( fopen(“foo.txt”,”w”), [](FILE* fp){fclose(fp);}); // 後始末のしかたをlambdaで与える fprintf(fp.get(), “Hello, worldn”); // 自動的に fclose! }

×