c++ - 为 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。
解决方案
推荐阅读
- php - 它没有给出错误,但它没有插入数据库
- python - CMS (Python/Django) 用户在“创建文章”页面上输入 Web 链接。这是怎么做到的?
- flash - 透明度变成蓝色,闪光
- json - Wordpress API:如何在 VueJS 中按 ID 获取类别名称?
- python - 从子节点之后的 XML 节点中提取文本
- recursion - 为什么我不能在循环内使用 (if (last acc) acc) 作为参数之一?
- html - 如何在这个 HTML 页面中居中这 4 个按钮?
- ms-access - 在 ACCESS vba SQL 中找不到类型不匹配错误
- oracle - 如何在 Update 语句中循环?
- c++ - Classsname instance[count]; 有什么区别?和 C++ 中的 Classname instance = new instance[count]?