首页 > 解决方案 > 为 Winapi 制作 typedef 时无法将 INVALID_HANDLE_VALUE 分配给 constexpr HANDLE

问题描述

我编写了以下代码以使我的代码不那么难看,并且不必担心过于激进地将 Windows 实现与其余代码分开。

#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

如果包含在 C++ 文件中,则无法在 Visual Studio 中编译:

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

在 G++ 中,它编译得很好。这个错误甚至意味着什么?

如何正确地将窗口INVALID_HANDLE_VALUE转换为constexpr void*,以及如何在上面代码的非窗口分支中执行相同的操作?

我的 Visual Studio 版本是 15.9.18。

标签: c++visual-studio-2017c++17cl

解决方案


推荐阅读