c++ - 编译时windows版一厢情愿
问题描述
为了准确显示我想要的内容,基本上我已经从这里获取了代码,并将其转换为一厢情愿,如下所示:
int wmain( int argc, PCWSTR argv[] )
{
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
if constexpr (IsWindowsXPOrGreater())
{
printf("XPOrGreater\n");
}
else if constexpr (IsWindowsXPSP1OrGreater())
{
printf("XPSP1OrGreater\n");
}
else if constexpr (IsWindowsXPSP2OrGreater())
{
printf("XPSP2OrGreater\n");
}
else if constexpr (IsWindowsXPSP3OrGreater())
{
printf("XPSP3OrGreater\n");
}
else if constexpr (IsWindowsVistaOrGreater())
{
printf("VistaOrGreater\n");
}
else if constexpr (IsWindowsVistaSP1OrGreater())
{
printf("VistaSP1OrGreater\n");
}
else if constexpr (IsWindowsVistaSP2OrGreater())
{
printf("VistaSP2OrGreater\n");
}
else if constexpr (IsWindows7OrGreater())
{
printf("Windows7OrGreater\n");
}
else if constexpr (IsWindows7SP1OrGreater())
{
printf("Windows7SP1OrGreater\n");
}
else if constexpr (IsWindows8OrGreater())
{
printf("Windows8OrGreater\n");
}
else if constexpr (IsWindows8Point1OrGreater())
{
printf("Windows8Point1OrGreater\n");
}
else if constexpr (IsWindows10OrGreater())
{
printf("Windows10OrGreater\n");
}
else if constexpr (IsWindowsServer())
{
printf("Server\n");
}
else
{
printf("Client\n");
}
}
目的显然是拥有标准的 C++ 代码,constexpr-if
与 windows 版本的编译时间检测一起使用。一切都在那里,VersionHelpers.h
但不是编译时 C++17。
, 中的函数VersionHelpers.h
不返回 constexpr 值。问题是是否有人做过这样的事情?或者不同的实现来实现相同的目标?
解决方案
这基本上是不可能的。
尽管库/操作系统版本有时会对部署施加实际限制,但编译器通常针对的是体系结构,而不是操作系统
如果您在计算机 A 上构建代码,编译器如何知道运行编译程序的计算机 B 的操作系统?
所以这就是没有一个是的原因之一constexpr
。
这是对更多运行时途径的探索。
推荐阅读
- unity3d - 检测网格上图像大小与网格大小的不同方法
- reactjs - React 在服务器上存储图像的最佳实践
- maven - 如何在特定的 Maven 目标之前添加属性验证?
- go - Endorsers 实例化 Chaincode 错误“找不到包”
- mongodb - 无法导入 URI。无法查找主机 cluster0-ohzuo.mongodb.net 的 TXT 记录
- swift - @IBOutlet 在选择器方法中为零
- android - Moshi 适配器创建失败:“需要显式注册 JsonAdapter”
- javascript - 数组列表获取替代结果的总和
- html - div 行没有正确对齐所有按钮
- regex - RegEx vb.net MatchCollection 充满了空字符串