我想消除代码中对 #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) 我想对来自 的 IP 地址进行数学运算boost::asio。具体来说,我需要在现有地址上增加和/或添加给定的整数,以简化我正在编写的软件中的 IP 列表生成。
就像今天一样,我使用自定义类来处理 IP 地址和网络,但我想转向boost::asio这样我就可以依靠它们来正确检查/验证地址和网络属性。
我有这个工作代码作为示例(除了该operator+部分):
using boost::asio::ip::make_address_v4;
using boost::asio::ip::address_v4;
auto add(const address_v4& address, std::size_t value = 1) {
return make_address_v4(address.to_uint() + value);
}
// This is definitely wrong
auto operator+(std::size_t value, const address_v4& address) {
return add(address, value);
}
int main() {
auto ip1 = make_address_v4("192.168.1.1");
fmt::print("IP address: {}\n", ip1.to_string());
auto ip2 = make_address_v4(ip1.to_uint() + 1);
fmt::print("IP address: {}\n", ip2.to_string());
// Add function
fmt::print("Added IP: {}\n", add(ip2, 8).to_string());
fmt::print("Added …Run Code Online (Sandbox Code Playgroud)