本文介绍了类似“ if constexpr”的东西但对于类定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如果constexpr 是摆脱C ++程序中预处理器的重要一步。但是,它仅适用于函数-如本例所示: 枚举类OS { Linux, MacOs, MsWindows,未知}; #如果已定义(__APPLE__) constexpr OS os = OS :: MacOs; #elif已定义(__MINGW32__) constexpr OS os = OS :: MsWindows; #elif定义(__linux__) constexpr OS os = OS :: Linux; #else constexpr OS os = OS :: Unknown; #endif void printSystem() { if constexpr(os == OS :: Linux) { std :: cout<< Linux; } 否则,如果constexpr(os == OS :: MacOs) { std :: cout<< 苹果系统; } 如果constexpr(os == OS :: MsWindows) { std :: cout<< MS Windows; } 其他 { std :: cout<< 未知操作系统; } } 但是摆脱预处理器的梦想并不十分令人满意-因为以下示例无法编译: 1 无法在类定义中使用它来不同地定义某些类成员: 类OsProperties { public: static void printName() { std :: cout<< osName; } 私有:如果constexpr(os == OS :: Linux) { const char * const osName = Linux; } 否则,如果constexpr(os == OS :: MacOs) { const char * const osName = MacOS; } 否则,如果constexpr(os == OS :: MsWindows) { const char * const osName = MS Windows; } else { const char * const osName =未知; } }; 2 也不适用于非类范围: 如果constexpr(os == OS :: Linux) { const char * const osName = Linux; } 否则,如果constexpr(os == OS :: MacOs) { const char * const osName = MacOS; } 否则,如果constexpr(os == OS :: MsWindows) { const char * const osName = MS Windows; } else { const char * const osName =未知; } 我(几乎)确信这是根据C ++ 17规范, code>如果constexpr 仅在函数体内起作用-但我的问题是: 第一季度在函数中实现类似于 if-constexpr 的效果-对于C ++ 1z / C ++ 14中的类和全局范围?而且我在这里不是要对模板专业化做进一步的解释……但是,它与 if constexpr ... $具有相似的简单性b $ b 第二季度是否有计划为上述范围扩展C ++?解决方案 如何在函数中实现类似if-constexpr的效果-对于C ++ 1z / C ++ 14中的类和全局作用域?而且我在这里不是要对模板专业化做进一步的解释…… 您基本上只是说,我想要模板专业化,但是没有那么讨厌的 template specialization 。 如果constexpr 是用于根据编译时构造使函数的行为发生变化。模板专业化是C ++提供的工具,用于基于编译时结构进行 definitions 更改。这是C ++唯一提供此功能的工具。 现在,您可以简单地初始化变量,您始终可以创建和调用lambda。 C ++ 17为lambda提供 constexpr 支持,如果constexpr ,lambda将能够使用来决定什么值返回。 是否有计划为上述范围扩展C ++? 否这里所有的提议,但没有一个 他们极不可能这样做。 if constexpr is a big step for getting rid of preprocessor in C++ programs. However it works only in functions - like in this example: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;#elseconstexpr OS os = OS::Unknown;#endifvoid 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 { std::cout << "Unknown-OS"; }}But dreams about getting rid of preprocessor are not quite satisfied - because the following examples do not compile:1 Cannot use it in class definition to define some members of class differently:class OsProperties{public: static void printName() { std::cout << osName; }private: if constexpr (os == OS::Linux) { const char* const osName = "Linux"; } else if constexpr (os == OS::MacOs) { const char* const osName = "MacOS"; } else if constexpr (os == OS::MsWindows) { const char* const osName = "MS Windows"; } else { const char* const osName = "Unknown"; }};2 Nor it works for not class-scope:if constexpr (os == OS::Linux){ const char* const osName = "Linux";}else if constexpr (os == OS::MacOs){ const char* const osName = "MacOS";}else if constexpr (os == OS::MsWindows){ const char* const osName = "MS Windows";}else{ const char* const osName = "Unknown";}I am (almost) sure this is per C++17 specification that if constexpr works only within function bodies - but my questions are:Q1 How to achieve the similar effect like if-constexpr in functions - for class and global scope in C++1z/C++14? And I am not asking here for yet another explanation of template specialization... But something that has similar simplicity as if constexpr...Q2 Are there any plan to extend C++ for the above mentioned scopes? 解决方案 How to achieve the similar effect like if-constexpr in functions - for class and global scope in C++1z/C++14? And I am not asking here for yet another explanation of template specialization...You basically just said, "I want template specialization, but without all that pesky template specialization."if constexpr is the tool for making the behavior of functions change based on compile-time constructs. Template specialization is the tool that C++ provides for making definitions change based on compile-time constructs. It is the only tool C++ provides for this functionality.Now for your simplistic case of initializing a variable, you can always create and call a lambda. C++17 offers constexpr support for lambdas, and a lambda would be able to use if constexpr to decide what value to return. Are there any plan to extend C++ for the above mentioned scopes?No. Here are all of the proposals, and none of the ones from the past couple of years delve into this domain.And it's highly unlikely they ever will. 这篇关于类似“ if constexpr”的东西但对于类定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 08:55
查看更多