The Idea
The idea is to be able to hold raw pointers until we find a smart pointer to hold them.
We shall make nullptr_t comparaison easier as well.
Original Implementation (Online link)
The code is available under this Godbolt link:
https://godbolt.org/z/T917eaojK
Original Implementation (For archiving purposes)
/// @author Amlal El Mahrouss (amlalelmahrouss@icloud.com)
/// @brief Pointer Type container.
using nullptr_t = decltype(nullptr);
namespace ns {
template<typename T>
class pointer_type;
template<typename T>
class pointer_type {
T* ptr_{nullptr};
public:
pointer_type() = delete;
pointer_type(T* in) : ptr_(in) {}
pointer_type& operator=(const pointer_type&) = delete;
pointer_type(const pointer_type&) = delete;
T* leak() noexcept {
auto ptr = ptr_;
ptr_ = nullptr;
return ptr;
}
public:
friend bool operator!=(const nullptr_t& rhs, const pointer_type& lhs) {
return lhs.ptr_ != static_cast<T*>(rhs);
}
friend bool operator!=(const pointer_type& lhs, const nullptr_t& rhs) {
return lhs.ptr_ != static_cast<T*>(rhs);
}
friend bool operator==(const nullptr_t& rhs, const pointer_type& lhs) {
return lhs.ptr_ == static_cast<T*>(rhs);
}
friend bool operator==(const pointer_type& lhs, const nullptr_t& rhs) {
return lhs.ptr_ == static_cast<T*>(rhs);
}
friend bool operator!=(const pointer_type& rhs, const pointer_type& lhs) {
return lhs.ptr_ != rhs.ptr_;
}
friend bool operator==(const pointer_type& lhs, const pointer_type& rhs) {
return lhs.ptr_ == rhs.ptr_;
}
};
}
int main() {
ns::pointer_type<int> ptr{nullptr};
ns::pointer_type<int> ptr_56{new int(56)};
return ptr_56 == ptr;
}