cppcheck 对 const std::string[] 抛出警告

cppcheck throws warning on const std::string[]

我正在努力应对 cppcheck(Linux 机器上的版本 1.85)正在报告的警告:

someFile.h:23:29: warning: Redundant code: Found a statement that begins with string constant. [constStatement]
const std::string OffOn[]= {"off", "on"};
^

我做了一些研究,发现将语句更改为

const std::string OffOn[]= {std::string("off"), std::string("on")};

删除警告。但是我不明白发生了什么,以及我的第一个解决方案 "bad" 是什么。也许有人可以向我解释一下?或者给我一些提示!

建议您使用 braced-init-list 初始化,例如:const std::string OffOn[]{"off", "on"};,因此 = 是不必要的。