C++ Logo

std-proposals

Advanced search

Re: Improving the usability/efficiency of unique_resource

From: Jim Porter <jporterbugs_at_[hidden]>
Date: Fri, 6 Aug 2021 09:09:48 -0700
On 8/6/2021 1:11 AM, Marcin Jaczewski via Std-Proposals wrote:
> pt., 6 sie 2021 o 03:26 Jim Porter via Std-Proposals
>> One area this *doesn't* address is when a handle type has a range of
>> invalid values, e.g. all values < 0. However, I'm not sure this is a
>> common enough case that the standard should account for it.
>>
> windows file handle have multiple error values:
> https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
> Some cases are -1 and other 0
> I recall that some other Win32 api could return multiple error value
> in the handle.

While confusing, I don't think this actually poses (much of) a problem
for standardization. Based on the examples, while there are multiple
invalid values in general, there's still a single invalid value for any
one function. Thus, while a user couldn't make a generic
`unique_win32_resource`, they could make `unique_win32_file_resource`,
`unique_win32_thread_resource`, etc. That's not perfect, but I doubt
there are many situations where you really want to store a thread *or* a
file handle in a `unique_resource`.

The real problem would be a function that returns, say, positive values
for valid handles and negative values for errors. I have a vague
recollection of some Win32 call that does this, but I couldn't find any
such examples, so I think it should be rare enough that the standard can
ignore it. (Working around the issue isn't terribly hard anyway; you
just need to wrap the handle-creation function so that it returns a
single invalid value.)

- Jim

Received on 2021-08-06 11:09:56