constexpr和之间有什么区别const?
我想消除代码中对 #define 宏的依赖,但我无法使用constexpr.
为了实用,请考虑以下示例:
\n#define PRODUCT_NAME "CloysterHPC"\nconstexpr const char* productName = PRODUCT_NAME;\n\nclass Newt : public View {\nprivate:\n struct TUIText {\n\n#if __cpp_lib_constexpr_string >= 201907L\n static constexpr const char* title =\n fmt::format("{} Installer", productName).data();\n#else\n static constexpr const char* title = PRODUCT_NAME " Installer";\n#endif\n\n };\n};\nRun Code Online (Sandbox Code Playgroud)\n我经历了惨痛的教训才知道fmt::format()函数不是constexpr函数,它只是一个运行时函数。我本以为我可以在代码中更具表现力,但我不能。所以我尝试使用std::string,但在将代码更改为类似以下内容后,我再次得到了相同的确切结果:
#define PRODUCT_NAME "CloysterHPC"\nconstexpr const char* productName = PRODUCT_NAME;\n\nclass Newt : public View {\nprivate:\n struct TUIText {\n\n#if __cpp_lib_constexpr_string >= 201907L\n static constexpr const char* title = …Run Code Online (Sandbox Code Playgroud)