Mikko-Pentti Einari Eronen Mikko-Pentti Einari Eronen - 2 months ago 19
C++ Question

nullptr initial value and WinAPI NULL return

If I initialize a variable with a value of nullptr.
and then I fetch a WinAPI function in to it that may return a value NULL on failure, do I have to use NULL or can I still check nullptr when checking what ever the function failed or not?

if ( windowfunctionresult == nullptr )
{
return false;
}

Answer

According to cppreference:

The keyword nullptr denotes the pointer literal. It is a prvalue of type std::nullptr_t. There exist implicit conversions from nullptr to null pointer value of any pointer type and any pointer to member type. Similar conversions exist for any null pointer constant, which includes values of type std::nullptr_t as well as the macro NULL.

So nullptr and NULL will behave the same in the context of checking whether a pointer is null.

But you can also simply rely on the pointer-to-bool conversion:

if ( !windowfunctionresult )
{
  return false;
}
Comments