Cannot assign INVALID_HANDLE_VALUE to constexpr HANDLE when making typedefs for Winapi

  c++, c++17, cl, visual-studio-2017

I made the following code to make my code less ugly and to not have to worry about too aggressively separating windows implementations from the rest of the code.

#pragma once
#ifdef _WIN32
#include "windows.h"

namespace RawHttp
{
namespace Win
{

using Bool = BOOL;
using Handle = HANDLE;
using Dword = DWORD;
constexpr const Handle Null = NULL;
constexpr const Handle InvalidHandle = static_cast<Handle>(INVALID_HANDLE_VALUE);
}
}
#else
namespace RawHttp
{
namespace Win
{

using Bool = bool;
using Handle = void *;
using Dword = unsigned long;
constexpr const Handle Null = nullptr;
constexpr const Handle InvalidHandle = (Handle)(long long)-1;
}
}
#endif //_WIN32

If included in a C++ file, it fails to compile in Visual Studio:

windowswindowstypes.h(14): error C2131: expression did not evaluate to a constant
windowswindowstypes.h(14): note: failure was caused by unevaluable pointer value

In G++, it compiles just fine. What does that error even mean?

My Visual Studio Version is 15.9.18.

Source: Windows Questions C++

LEAVE A COMMENT