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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Relevant reference
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).
See MSDN's Old New Thing blog for more details: Why are HANDLE return values so inconsistent?
You guys definitely want to read this: When MSDN says NULL, is it okay to use nullptr? reference
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.
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.

4 Comments

I'm pretty sure some of them default to struct HWND__ * or something unless a #define is there.
@chris: That would be STRICT
@MSalters, Ah, that sounds right now that you mention it.
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.