gpt4 book ai didi

c++ - 头文件中的 static const std::string

转载 作者:行者123 更新时间:2023-11-28 06:07:33 24 4
gpt4 key购买 nike

在我的 CMake 构建系统中,我自动生成了一个头文件。以前,我在做这样的事情

// old_auto_gen_file.hpp
#define VERSION_STRING "@VERSION_STRING@"
#define VERSION_MAJOR @VERSION_MAJOR@

通过 CMake 填充 @...@ 的地方。

如果可能的话,我想丢掉 #define。我在想这样的事

// new_auto_gen_file.hpp
static const std::string VERSION_STRING("@VERSION_STRING@");
static const int VERSION_MAJOR(@VERSION_MAJOR@);

但是,我很困惑这是否合法?这对 C++11 来说很酷吗?

最佳答案

这没有任何问题,除了我看不出有任何理由为 std::string 的小开销而烦恼。一个简单的 char 数组就足够了:

static const char VERSION_STRING[]="@VERSION_STRING@";
static const int VERSION_MAJOR=@VERSION_MAJOR@;

关于c++ - 头文件中的 static const std::string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32085482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com