Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::unique_ptr ABI-compatible with raw pointers?

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?

like image 212
Shea Levy Avatar asked Jul 07 '26 01:07

Shea Levy


1 Answers

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.

like image 90
Oliver Charlesworth Avatar answered Jul 09 '26 14:07

Oliver Charlesworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!