|
1 | | -/* |
2 | | -# unique_ptr |
3 | | -
|
4 | | -Sample use case: |
5 | | -
|
6 | | -- you want a dynamic array a dynamically allocated derived class |
7 | | -- thus you have to use pointers for polymorphism. Otherwise objects can have different sizes, and arrays can't be used. |
8 | | -- how to prevent memory leaks? |
9 | | -
|
10 | | -http://en.cppreference.com/w/cpp/memory/unique_ptr |
11 | | -
|
12 | | -unique_ptr may incur an extra dereferece cost, but it is usually well worth it. |
13 | | -
|
14 | | -In Java, everything can be though as a smart pointer (shared), |
15 | | -so using this is still more efficient than Java, since C++ can know the exact |
16 | | -lifetime of objects, and release them immediately when they are done. |
17 | | -*/ |
| 1 | +/* # unique_ptr |
| 2 | + * |
| 3 | + * Sample use case: |
| 4 | + * |
| 5 | + * - you want a dynamic array a dynamically allocated derived class |
| 6 | + * - thus you have to use pointers for polymorphism. Otherwise objects can have different sizes, and arrays can't be used. |
| 7 | + * - how to prevent memory leaks? |
| 8 | + * |
| 9 | + * http://en.cppreference.com/w/cpp/memory/unique_ptr |
| 10 | + * |
| 11 | + * unique_ptr may incur an extra dereferece cost, but it is usually well worth it. |
| 12 | + * |
| 13 | + * In Java, everything can be though as a smart pointer (shared), |
| 14 | + * so using this is still more efficient than Java, since C++ can know the exact |
| 15 | + * lifetime of objects, and release them immediately when they are done. |
| 16 | + */ |
18 | 17 |
|
19 | 18 | #include "common.hpp" |
20 | 19 |
|
@@ -179,24 +178,23 @@ int main() { |
179 | 178 | assert(Base::count == 0); |
180 | 179 | } |
181 | 180 |
|
182 | | - /* |
183 | | - # unique_ptr function arguments |
184 | | -
|
185 | | - - transferring ownership TODO examples |
186 | | - - use raw pointers on the interface, and convert it to unique_ptr inside callee |
187 | | - - if you already have an unique_ptr, release() it |
188 | | - - this allows you to not tie down to a specific smart pointer on the function interface |
189 | | - - use unique_ptr on interface and move on caller. |
190 | | - - Advantage: unique_ptr on interface documents ownership transfer, |
191 | | - and prevents callee from passing non new pointer to it by mistake. |
192 | | - - TODO for not transferring ownership: |
193 | | - - `const & std::unique_ptr<T>` |
194 | | - - `get()`. Simple and efficient. But how to use it for containers like `vector<std::unique_ptr>`? |
195 | | - - `T&` on function, `*t` on caller. Looks good! |
196 | | -
|
197 | | - - http://stackoverflow.com/questions/8114276/how-do-i-pass-a-unique-ptr-argument-to-a-constructor-or-a-functionhttp://stackoverflow.com/questions/8114276/how-do-i-pass-a-unique-ptr-argument-to-a-constructor-or-a-function |
198 | | - - http://stackoverflow.com/questions/11277249/how-to-pass-stdunique-ptr-around |
199 | | - */ |
| 181 | + /* # unique_ptr function arguments |
| 182 | + * |
| 183 | + * - transferring ownership TODO examples |
| 184 | + * - use raw pointers on the interface, and convert it to unique_ptr inside callee |
| 185 | + * - if you already have an unique_ptr, release() it |
| 186 | + * - this allows you to not tie down to a specific smart pointer on the function interface |
| 187 | + * - use unique_ptr on interface and move on caller. |
| 188 | + * - Advantage: unique_ptr on interface documents ownership transfer, |
| 189 | + * and prevents callee from passing non new pointer to it by mistake. |
| 190 | + * - TODO for not transferring ownership: |
| 191 | + * - `const & std::unique_ptr<T>` |
| 192 | + * - `get()`. Simple and efficient. But how to use it for containers like `vector<std::unique_ptr>`? |
| 193 | + * - `T&` on function, `*t` on caller. Looks good! |
| 194 | + * |
| 195 | + * - http://stackoverflow.com/questions/8114276/how-do-i-pass-a-unique-ptr-argument-to-a-constructor-or-a-functionhttp://stackoverflow.com/questions/8114276/how-do-i-pass-a-unique-ptr-argument-to-a-constructor-or-a-function |
| 196 | + * - http://stackoverflow.com/questions/11277249/how-to-pass-stdunique-ptr-around |
| 197 | + */ |
200 | 198 | { |
201 | 199 | } |
202 | 200 |
|
|
0 commit comments