On 12/4/23 22:38, Bruno MartÃnez via Boost wrote:
Hi,
unique_ptr can emulate unique_resource:
template
class unique { using T = decltype(nullvalue); struct generic_delete { class pointer { T t; public: pointer(T t) : t(t) {} pointer(std::nullptr_t = nullptr) : t(nullvalue) { } explicit operator bool() { return t != nullvalue; } friend bool operator ==(pointer lhs, pointer rhs) { return lhs.t == rhs.t; } friend bool operator !=(pointer lhs, pointer rhs) { return lhs.t != rhs.t; } operator T() { return t; } };
void operator()(T p) { delete_(p); } }; public: using type = std::unique_ptr
; }; int main() { using unique_fd = unique<-1, close>::type; static_assert(sizeof(unique_fd) == sizeof(int), "bloated unique_fd"); unique_fd fd1(open("fd.txt", O_WRONLY | O_CREAT | O_TRUNC)); write(fd1.get(), "hello\n", 6); }
The std papers say
While std::unique_ptr can be tweaked by using a custom deleter type to almost a perfect handler for resources, it is awkward to use for handle types that are not pointers.
But what's the awkwardness?
You are hijacking a tool that was not intended to be used this way. unique_ptr is only supposed to be used with pointers and pointer-like objects, which is why it accepts nullptr in its interface. The syntax: fd1 = nullptr; is allowed, but it doesn't make sense for the reader who expects fd1 to be a file descriptor. This would be especially confusing if the resource type was a pointer type, with the unallocated value being something else than a null pointer. Case in point, Windows' HANDLE and INVALID_HANDLE_VALUE.