cppcheck throws warning on const std::string[]
Clash Royale CLAN TAG#URR8PPP
up vote
9
down vote
favorite
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!
c++ cppcheck
New contributor
Mukuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
9
down vote
favorite
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!
c++ cppcheck
New contributor
Mukuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Maybe it recomeds you to use initializer_list like:const std::string OffOn"off", "on";
, so=
is just unnecessary.
– Denis Sablukov
4 hours ago
@DenisSablukov that's it! And now it make perfectly sense to me. Thanks a lot!
– Mukuma
4 hours ago
2
I'd actually file an issue report to cppcheck. The warning message produced is really unhelpful.
– Ruslan
1 hour ago
add a comment |Â
up vote
9
down vote
favorite
up vote
9
down vote
favorite
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!
c++ cppcheck
New contributor
Mukuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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!
c++ cppcheck
c++ cppcheck
New contributor
Mukuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Mukuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 9 mins ago
Null
1,46361926
1,46361926
New contributor
Mukuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 4 hours ago
Mukuma
483
483
New contributor
Mukuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Mukuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Mukuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Maybe it recomeds you to use initializer_list like:const std::string OffOn"off", "on";
, so=
is just unnecessary.
– Denis Sablukov
4 hours ago
@DenisSablukov that's it! And now it make perfectly sense to me. Thanks a lot!
– Mukuma
4 hours ago
2
I'd actually file an issue report to cppcheck. The warning message produced is really unhelpful.
– Ruslan
1 hour ago
add a comment |Â
1
Maybe it recomeds you to use initializer_list like:const std::string OffOn"off", "on";
, so=
is just unnecessary.
– Denis Sablukov
4 hours ago
@DenisSablukov that's it! And now it make perfectly sense to me. Thanks a lot!
– Mukuma
4 hours ago
2
I'd actually file an issue report to cppcheck. The warning message produced is really unhelpful.
– Ruslan
1 hour ago
1
1
Maybe it recomeds you to use initializer_list like:
const std::string OffOn"off", "on";
, so =
is just unnecessary.– Denis Sablukov
4 hours ago
Maybe it recomeds you to use initializer_list like:
const std::string OffOn"off", "on";
, so =
is just unnecessary.– Denis Sablukov
4 hours ago
@DenisSablukov that's it! And now it make perfectly sense to me. Thanks a lot!
– Mukuma
4 hours ago
@DenisSablukov that's it! And now it make perfectly sense to me. Thanks a lot!
– Mukuma
4 hours ago
2
2
I'd actually file an issue report to cppcheck. The warning message produced is really unhelpful.
– Ruslan
1 hour ago
I'd actually file an issue report to cppcheck. The warning message produced is really unhelpful.
– Ruslan
1 hour ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
10
down vote
accepted
It recommends you use initializer_list
like: const std::string OffOn"off", "on";
, so =
is just unnecessary.
2
It's probably nitpicking, but in this context...
is not astd::initializer_list
. It's abraced-init-list
.
– HolyBlackCat
1 hour ago
It's clearly a bug regardless; there's no "redundant code" here
– Lightness Races in Orbit
10 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
It recommends you use initializer_list
like: const std::string OffOn"off", "on";
, so =
is just unnecessary.
2
It's probably nitpicking, but in this context...
is not astd::initializer_list
. It's abraced-init-list
.
– HolyBlackCat
1 hour ago
It's clearly a bug regardless; there's no "redundant code" here
– Lightness Races in Orbit
10 mins ago
add a comment |Â
up vote
10
down vote
accepted
It recommends you use initializer_list
like: const std::string OffOn"off", "on";
, so =
is just unnecessary.
2
It's probably nitpicking, but in this context...
is not astd::initializer_list
. It's abraced-init-list
.
– HolyBlackCat
1 hour ago
It's clearly a bug regardless; there's no "redundant code" here
– Lightness Races in Orbit
10 mins ago
add a comment |Â
up vote
10
down vote
accepted
up vote
10
down vote
accepted
It recommends you use initializer_list
like: const std::string OffOn"off", "on";
, so =
is just unnecessary.
It recommends you use initializer_list
like: const std::string OffOn"off", "on";
, so =
is just unnecessary.
edited 4 hours ago
Toby Speight
15.7k133864
15.7k133864
answered 4 hours ago


Denis Sablukov
437413
437413
2
It's probably nitpicking, but in this context...
is not astd::initializer_list
. It's abraced-init-list
.
– HolyBlackCat
1 hour ago
It's clearly a bug regardless; there's no "redundant code" here
– Lightness Races in Orbit
10 mins ago
add a comment |Â
2
It's probably nitpicking, but in this context...
is not astd::initializer_list
. It's abraced-init-list
.
– HolyBlackCat
1 hour ago
It's clearly a bug regardless; there's no "redundant code" here
– Lightness Races in Orbit
10 mins ago
2
2
It's probably nitpicking, but in this context
...
is not a std::initializer_list
. It's a braced-init-list
.– HolyBlackCat
1 hour ago
It's probably nitpicking, but in this context
...
is not a std::initializer_list
. It's a braced-init-list
.– HolyBlackCat
1 hour ago
It's clearly a bug regardless; there's no "redundant code" here
– Lightness Races in Orbit
10 mins ago
It's clearly a bug regardless; there's no "redundant code" here
– Lightness Races in Orbit
10 mins ago
add a comment |Â
Mukuma is a new contributor. Be nice, and check out our Code of Conduct.
Mukuma is a new contributor. Be nice, and check out our Code of Conduct.
Mukuma is a new contributor. Be nice, and check out our Code of Conduct.
Mukuma is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53099306%2fcppcheck-throws-warning-on-const-stdstring%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Maybe it recomeds you to use initializer_list like:
const std::string OffOn"off", "on";
, so=
is just unnecessary.– Denis Sablukov
4 hours ago
@DenisSablukov that's it! And now it make perfectly sense to me. Thanks a lot!
– Mukuma
4 hours ago
2
I'd actually file an issue report to cppcheck. The warning message produced is really unhelpful.
– Ruslan
1 hour ago