相关疑难解决方法(0)

`constexpr`和`const`之间的区别

constexpr和之间有什么区别const

  • 我什么时候才能只使用其中一个?
  • 我何时可以同时使用这两种方法?如何选择?

c++ const constexpr c++11

540
推荐指数
9
解决办法
19万
查看次数

在必须连接两个 const char* 变量的情况下,如何避免在 C++ 中使用 #define 宏?

我想消除代码中对 #define 宏的依赖,但我无法使用constexpr.

\n

为了实用,请考虑以下示例:

\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};\n
Run Code Online (Sandbox Code Playgroud)\n

我经历了惨痛的教训才知道fmt::format()函数不是constexpr函数,它只是一个运行时函数。我本以为我可以在代码中更具表现力,但我不能。所以我尝试使用std::string,但在将代码更改为类似以下内容后,我再次得到了相同的确切结果:

\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 = …
Run Code Online (Sandbox Code Playgroud)

c++ constexpr string-view c++20 fmt

13
推荐指数
2
解决办法
1714
查看次数

标签 统计

c++ ×2

constexpr ×2

c++11 ×1

c++20 ×1

const ×1

fmt ×1

string-view ×1