修复字符串的疯狂非 pod-global-static 警告

问题描述 投票:0回答:1

Clazy 输出非 pod-global-statics 的警告。我明白为什么这会减慢代码速度,但不知道更好的解决方案是什么?例如,在这个简单的头文件中:

模块1.hpp

#include <string>
#include <vector>

const std::vector<std::string> namesVec{"name1", "name2", "name3"};
static const std::string name{"name1"};

class Module1
{
  public:
    Module1();
    ~Module1();

    void func1();
};

其中有两个警告,分别针对

namesVec
name
。避免这些警告的最佳方法是什么?

c++ static-analysis
1个回答
0
投票

使用字符串文字直接创建 std::string,而不是从 const char* 创建并复制它。

using namespace std::string_literals;

static const auto mystr = "hello"s; 
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.