cppcheck在const std::string[]上引发警告

cppcheck throws warning on const std::string[]

本文关键字:警告 string const std cppcheck      更新时间:2023-10-16

我正在努力处理cppcheck(Linux机器上的1.85版本(正在报告的警告:

someFile.h:23:29:warning:冗余代码:找到一个以字符串常量开头的语句。constStatement]
conststd::string OffOn[]={"off","on"}
^

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

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

删除警告。然而,我不明白发生了什么,也不明白我的第一个解决方案有什么"糟糕"之处。也许有人能向我解释一下?或者给我一些提示!

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