Timeline for Is there a standard way to indicate that a function returns a new pointer?
Current License: CC BY-SA 3.0
8 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Dec 23, 2015 at 3:34 | comment | added | 5gon12eder | If the one who calls new_vertex totally cannot live with a std::unique_ptr, they can always get back the raw pointer by calling its release member function. But the function's interface will be safe and explicit. I bet you won't be able to measure the performance difference. | |
| Jul 19, 2015 at 1:09 | comment | added | Deduplicator | @immibis: Why fo you override the template-argument-deduction of std::make_unique? That's unneccessarily verbose and error-prone... | |
| Jul 15, 2015 at 14:43 | comment | added | Grimm The Opiner | @Shep there's no harm returning a raw pointer in this case. This case being that you've created an object that it is up to the caller to manage the lifetime of. The caller can always drop it straight into a smart pointer if they think they need to. | |
| Jul 15, 2015 at 13:51 | comment | added | YoungJohn | unique_ptr is also convertible to shared_ptr (though not vice versa) so a function returning a unique_ptr can still have the result moved to a shared_ptr if shared ownership is required. | |
| Jul 15, 2015 at 9:05 | comment | added | ComicSansMS | @Shep Names are intangible things and everyone is free to ignore them completely. Types on the other hand are enforced by the compiler, which means it requires significant effort to break stuff. Never send a name to do a type's job. | |
| Jul 15, 2015 at 7:32 | comment | added | Stack Exchange Broke The Law | @Shep If you want to indicate to the person calling your function that it creates a new object, unique_ptr does that. If it has to be a name for some reason, then I guess it doesn't, but why does it have to be a name? | |
| Jul 15, 2015 at 7:30 | comment | added | Shep | This is helpful, but it doesn't really answer my question. I know about smart pointers, I just wanted to know if there's a naming convention for functions that return raw pointers that the user has to take care of. I guess the answer is "no, there isn't, because you should never do that"? | |
| Jul 15, 2015 at 3:20 | history | answered | Stack Exchange Broke The Law | CC BY-SA 3.0 |