Search code examples
c++cppcheck

cppcheck throws warning on const std::string[]


I'm struggling with a warning that cppcheck (version 1.85 on a Linux machine) is reporting:

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

I did some research and found that changing the statement to

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

removes the warning. However I do not understand what's going on, and what's "bad" about my first solution. Maybe someone can explain it to me? Or give me some hints!


Solution

  • It recommends you use initialization with a braced-init-list like: const std::string OffOn[]{"off", "on"};, so = is just unnecessary.