相关疑难解决方法(0)

像"if constexpr"之类的东西,但是用于类定义

if constexpr是在C++程序中摆脱预处理器的重要一步.但它仅适用于函数 - 如下例所示:

enum class OS
{
    Linux,
    MacOs,
    MsWindows,
    Unknown
};

#if defined(__APPLE__)
constexpr OS os = OS::MacOs;
#elif defined(__MINGW32__)
constexpr OS os = OS::MsWindows;
#elif defined(__linux__)
constexpr OS os = OS::Linux;
#else
constexpr OS os = OS::Unknown;
#endif

void printSystem()    
{
    if constexpr (os == OS::Linux)
    {
        std::cout << "Linux";
    }
    else if constexpr (os == OS::MacOs)
    {
        std::cout << "MacOS";
    }
    else if constexpr (os == OS::MsWindows)
    {
        std::cout << "MS Windows";
    }
    else
    { …
Run Code Online (Sandbox Code Playgroud)

c++ class c-preprocessor c++17 if-constexpr

12
推荐指数
1
解决办法
2735
查看次数

标签 统计

c++ ×1

c++17 ×1

c-preprocessor ×1

class ×1

if-constexpr ×1