Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cppcheck throws warning on const std::string[]

Tags:

c++

cppcheck

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!

like image 877
Mukuma Avatar asked Nov 01 '18 10:11

Mukuma


1 Answers

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

like image 144
Denis Sablukov Avatar answered Nov 06 '22 15:11

Denis Sablukov