【发布时间】:2018-12-27 22:18:32
【问题描述】:
为了准确地展示我想要的东西,基本上我已经从here 中获取了代码,并将其转化为如下的一厢情愿:
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 值。问题是是否有人做过这样的事情?或者不同的实现来实现相同的目标?
【问题讨论】:
-
那么,您想检测您正在构建的计算机是某个版本还是您正在运行的计算机?因为后者是不可能做编译时的。
-
related/dupe: stackoverflow.com/questions/10112051/… 您可以使用链接中提供的信息来编写您自己的版本。
-
constexpr-if需要编译时间值,然而这个显然误解了这个 C++ 特性的“草帽鸡”却得到了 +2?我得到-2(到目前为止),我假设来自其他一些鸡...... -
最简单且通常最省力的方法是将自己限制在您计划支持的最旧操作系统版本中存在的 API 工具上。以我的经验,这不会显着限制您的能力,除非在某些边缘情况下,例如复杂的控制台 UI。 Microsoft 的文档对此非常有用,因为它清楚地指出了支持的最低版本。