My reading about std::unique_ptr
has convinced me that it (along with the other smart pointers) is definitely the best choice for pointers internally in a project (especially after Could an optimizing compiler remove all runtime costs from std::unique_ptr?), but I'm not sure yet about what to do at the interface. I don't fully grok how various C++ ABIs work, so please bear with me if this is a dumb question, but could functions that take or return pointers, classes/structs that have pointer members, etc. be replaced by something that uses unique_ptr
(maybe using unique_ptr
refs?) without requiring recompilation? Can C code interact with C++ interfaces using unique_ptr
without complication?
Asked
Active
Viewed 756 times
2
-
1Unless you are *transferring ownership* of memory to a function, it has no business taking a unique_ptr. Simply stop using `delete` anywhere and use smart pointers that have proper ownership semantics. A function that takes a naked pointer does not claim ownership of that pointer and should not access it outside of the function's scope (ie: storing it in a global or something). [Here's a survey of the ways to pass unique_ptrs and their meanings](http://stackoverflow.com/questions/8114276/how-do-i-pass-a-unique-ptr-argument-to-a-constructor-or-a-function/8114913#8114913). – Nicol Bolas Jul 22 '13 at 01:13
1 Answers
5
AFAIK, there's no guarantee that sizeof(unique_ptr<T>) == sizeof(T*)
, so no.
But even if it was the same size, there's also no guarantee that an arbitrary ABI will pass pointer arguments by the same mechanism as "value" arguments.

Oliver Charlesworth
- 267,707
- 33
- 569
- 680