samedi 14 décembre 2019

Deleting an untyped shared_ptr

I am working on wrapping a C++ library into a C bridge.

All objects, I’d like to maintain with shared_ptrs on the heap like:

void* makeFoo() {
  return <void*> new shared_ptr<Foo>(shared_ptr::make_shared<Foo>());
}

Can I use a generic destroy like this:

void destroy(void* p) {
  delete <shared_ptr<void>*> p;
}

Or is there a cleaner way?

EDIT

It looks like the cleanest solution is to have a generic wrapper that takes a lambda that deletes the typed shared_ptr by closing over it. A bit of memory waste though.

Aucun commentaire:

Enregistrer un commentaire