cppcheck throws wanring on const std::string[]

  • A+
Category:Languages

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 reading hints!

 


It recommends you use initializer_list like: const std::string OffOn[]{"off", "on"};, so = is just unnecessary.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: