Skip to content

Instantly share code, notes, and snippets.

@Voltra
Created August 18, 2023 21:15
Show Gist options
  • Save Voltra/27f82890c9d5098dda07f748de9f8eab to your computer and use it in GitHub Desktop.
Save Voltra/27f82890c9d5098dda07f748de9f8eab to your computer and use it in GitHub Desktop.
Opaque owning pointer
#include <memory>
template <class T, class Deleter = std::default_delete<T>>
class owning_ptr {
private:
T* ptr;
Deleter deleter;
explicit owning_ptr(T* rawPtr, Deleter d = {}) : ptr{rawPtr}, deleter{d} {}
const T* as_immutable() const { return ptr; }
T* as_mutable() { return ptr; }
~owning_ptr() {
deleter(ptr);
ptr = nullptr;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment