Timeline for Return a unique_ptr by reference
Current License: CC BY-SA 3.0
Post Revisions
8 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Apr 14, 2017 at 17:12 | vote | accept | Behrooz Karjoo | ||
| Apr 13, 2017 at 20:29 | answer | added | Xirema | timeline score: 7 | |
| Apr 13, 2017 at 20:28 | answer | added | Vittorio Romeo | timeline score: 10 | |
| Apr 13, 2017 at 20:19 | comment | added | AndyG |
+1 for the raw pointer solution. I will also add that it's very common to return iterators to collections for find-like operations. You'd return the end iterator on not found. Perhaps with C++17 people will start moving towards a std::optional approach, though.
|
|
| Apr 13, 2017 at 20:17 | comment | added | 463035818_is_not_an_ai | why dont you just return the iterator? | |
| Apr 13, 2017 at 20:10 | comment | added | Drew Dormann |
NathanOliver beat me by a few seconds. To his point, do you have a reason why you want to return a const unique_ptr reference? You are not changing ownership, so you probably don't need to expose how these orders are memory-managed.
|
|
| Apr 13, 2017 at 20:08 | comment | added | NathanOliver |
You could always return a regular, non owning raw pointer. That would allow you to replace return null_unique; with return nullptr;.
|
|
| Apr 13, 2017 at 20:05 | history | asked | Behrooz Karjoo | CC BY-SA 3.0 |