While this should be a trivial question I was unable to find an answer this far. In C APIs there are lots of functions that take pointers and pointers to pointers as arguments. How can I use PROPERLY smart pointers as arguments with C APIs.
Here is an example that I'd like to convert to using std::unique_ptr:
FMOD_SYSTEM* system = nullptr;
result = FMOD_System_Create(&system); // Create the main system object
FMOD_SOUND* musicStream;
result = FMOD_System_CreateSound(system,
musicStreamPath,
FMOD_CREATESTREAM,
nullptr,
&musicStream);
Reference: FMOD_System_Create FMOD_System_CreateSound
I start declaring the smart pointers as:
std::unique_ptr<FMOD_SYSTEM> system = nullptr;
std::unique_ptr<FMOD_SOUND> musicStream = nullptr;
Here are the compiler errors if I use .get():
cannot convert 'std::unique_ptr::pointer {aka FMOD_SOUND*}' to 'FMOD_SOUND**' for argument '5' to 'FMOD_RESULT FMOD_System_CreateSound(FMOD_SYSTEM*, const char*, FMOD_MODE, FMOD_CREATESOUNDEXINFO*, FMOD_SOUND**)' musicStream.get());
^
.get()method to get the underlying pointer?deleteon that pointer (pretty unlikely if it's returned by a C API), you have to supply a custom deleter.