2

I want to use a Windows HANDLE (a thread handle) as key in a std::map. For this, I need a less-than operator that is well-defined for the type HANDLE.

Pointer types T* can be used as keys for std::map only if the comparator std::less<T*> is specified, because the builtin operator<(T*, T*) (and henceforth std::less<>) does not provide a total order.

Implementationwise, we know that HANDLE is actually typedefed as a pointer type. Therefore, std::less<HANDLE> looks like a safe bet. But I consider it just an implementation detail that HANDLE is implemented as a pointer type. (And by inspecting actual HANDLE values in the debugger, it can be clearly seen that they are not genuine pointer values.)

Are there any formal guarantees that can be derived from the C++ standard and the Windows API documentation that would allow me to use HANDLE as a key in std::map?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
j6t
  • 9,150
  • 1
  • 15
  • 35
  • 4
    What's important is the *type*, not the actual *value*. A `HANDLE` really is of type `void*`. If a total order exists for `void*`, then you can use it as a key in a `std::map`. Keep in mind, that thread handles do get re-used after a thread object has been disposed. – IInspectable Feb 27 '20 at 12:33
  • @IInspectable Can you cite the documentation that states that `HANDLE` is a pointer type? – j6t Feb 27 '20 at 12:34
  • 4
    [Windows Data Types](https://learn.microsoft.com/en-us/windows/win32/winprog/windows-data-types). – IInspectable Feb 27 '20 at 12:35
  • 1
    Concerning _because the builtin `operator<(T*, T*)` (and henceforth `std::less<>`) does not provide a total order._ [SO: Universal less<> for pointers in C++ standard](https://stackoverflow.com/a/1099080/7478597) It's about C++03 but I really would wonder if such useful feature/requirement has been removed from more recent standards... ;-) – Scheff's Cat Feb 27 '20 at 13:05
  • 2
    @Scheff - Spoiler: it's true for every C++ version since. – StoryTeller - Unslander Monica Feb 27 '20 at 13:05
  • @j6t: "*Can you cite the documentation that states that HANDLE is a pointer type?*" Can you cite the documentation that states that HANDLEs have an order, rather than just being able to equality-test them? Because it seems to me that, by wanting to detect an order *at all*, you're already relying on the specifics of how a `HANDLE` is implemented. – Nicol Bolas Feb 27 '20 at 14:35
  • @NicolBolas Since `HANDLE` is documented to be `void*`, an order is implied. Yes, the implementers restrict themselves to a certain implementation (and so guarantee properties---such as an order---that would otherwise not be needed) by documenting it publically, but that is not my problem ;) On the contrary, it is very useful. – j6t Feb 27 '20 at 15:08
  • @j6t: So if it is documented to be a `void*`, and it is required by the C++ standard that pointers have a total order accessible through `std::less`... what's your problem? How many more "formal guarantees" do you need? – Nicol Bolas Feb 27 '20 at 16:10
  • @NicolBolas That's all the guarantees I want. Didn't I say that already...? – j6t Feb 27 '20 at 19:18

0 Answers0