nullptr 初始值和 WinAPI NULL return

nullptr initial value and WinAPI NULL return

如果我初始化一个值为 nullptr 的变量。 然后我向其中获取一个 WinAPI 函数,该函数在失败时可能 return 值为 NULL,我必须使用 NULL 还是在检查函数是否失败时仍然检查 nullptr?

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

根据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.

因此 nullptrNULL 在检查指针是否为空的上下文中表现相同。

但是你也可以简单地依赖指针到bool的转换:

if ( !windowfunctionresult )
{
  return false;
}