11

I know using nullptr is more "typed". It can distinguish pointer type and 0 and works well in function overloading and template specialization.

So I am not sure whether it is safe to replace the NULL to nullptr in my old Win32 project in every HANDLE/HWND/HINSTNACE initialization usages?

Any suggestion will be helpful. Thanks

Chen OT
  • 3,486
  • 2
  • 24
  • 46

2 Answers2

13

For handles that resolve to a pointer type you can use nullptr instead of NULL. A good number of handle types are typedef'd as pointers so you shouldn't run into much problem.

This does not mean it is ok to use either NULL or nullptr. Some calls return INVALID_HANDLE_VALUE which in VS2013 is defined as ((HANDLE)(LONG_PTR)-1) and relying on a null value to indicate an invalid/unopen handle may cause problems. For instance CreateFile returns INVALID_HANDLE_VALUE instead of a zero or null value. All places in your code that assumes a null value indicates an unopened handle may cause problems.

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
  • 1
    Relevant [reference](http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%29.aspx) – Casper Beyer Apr 23 '14 at 03:11
  • Most handles (Kernel HANDLE, GDI handles, USER handles) are defined as pointers, but some (such as, IIRC, the CryptoAPI handles) are integers instead and thus wouldn't accept `nullptr` (or `NULL` in C code). – Medinoc Apr 23 '14 at 08:12
  • 2
    See MSDN's Old New Thing blog for more details: [Why are HANDLE return values so inconsistent?](http://blogs.msdn.com/b/oldnewthing/archive/2004/03/02/82639.aspx) – Remy Lebeau Apr 23 '14 at 18:10
  • 1
    You guys definitely want to read this: **When MSDN says NULL, is it okay to use nullptr?** [reference](https://devblogs.microsoft.com/oldnewthing/20180307-00/?p=98175) – metablaster Oct 09 '19 at 18:47
  • Sorry but that article is a waste of time and can be summarized as "most people would probably prefer you to write NULL or nullptr.". This is not a compelling technical reason to follow the lemmings. – Captain Obvlious Oct 13 '19 at 14:56
3

Yes. HWND, HANDLE, etc., most resolve to void *, so there's no reason not to use nullptr. That being said, it may be deemed more consistent with the API to use NULL, but it will not make a difference.

smiling_nameless
  • 1,047
  • 1
  • 9
  • 23
  • 2
    I'm pretty sure some of them default to `struct HWND__ *` or something unless a `#define` is there. – chris Apr 23 '14 at 02:39
  • 1
    @chris: That would be [`STRICT`](http://msdn.microsoft.com/en-us/library/windows/desktop/aa383681(v=vs.85).aspx) – MSalters Apr 23 '14 at 07:59
  • @MSalters, Ah, that sounds right now that you mention it. – chris Apr 23 '14 at 09:24
  • And this would not work for `HANDLE` values that do not use `NULL`/`0` to indicate an *invalid* handle, like the `HANDLE` from `CreateFile()`. See MSDN's Old New Thing blog for more details: [Why are HANDLE return values so inconsistent?](http://blogs.msdn.com/b/oldnewthing/archive/2004/03/02/82639.aspx) – Remy Lebeau Apr 23 '14 at 18:09