Created
August 18, 2023 21:15
-
-
Save Voltra/27f82890c9d5098dda07f748de9f8eab to your computer and use it in GitHub Desktop.
Opaque owning pointer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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