3 ms·
Typically what I do in that situation is lightly wrap the API. For instance: Image* image = allocate_image(…); … deallocate_image(image); ↓
by evincarofautumn 14y ago
Typically what I do in that situation is lightly wrap the API. For instance:
Image* image = allocate_image(…);
…
deallocate_image(image);
↓
struct ImageDeleter {
void operator()(void* image) const {
deallocate_image(image);
}
};
unique_ptr<Image, ImageDeleter> image(allocate_image(…));
That certainly can’t work everywhere, but I have yet to be failed by it.
- jongraehl 14y agoPeople who don't do this are just asking for it.
- jmq 14y agoYou can reduce verbosity of the code by passing destructor function directly to unique_ptr constructor: std::unique_ptr<Image, void (*)(Image *)>(image, deallocate_image);
- evincarofautumn 14y agoCool, thanks for the tip. I meant to offer an example for the general case.
- basman 14y agoOr, perhaps easier: scoped_ptr<Image> image(allocate_image(...));