首页 > 解决方案 > 编译时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 值。问题是是否有人做过这样的事情?或者不同的实现来实现相同的目标?

标签: c++windowsc++17

解决方案


这基本上是不可能的。

尽管库/操作系统版本有时会对部署施加实际限制,但编译器通常针对的是体系结构,而不是操作系统

如果您在计算机 A 上构建代码,编译器如何知道运行编译程序的计算机 B 的操作系统?

所以这就是没有一个是的原因之一constexpr

这是对更多运行时途径的探索。


推荐阅读