cppcheck throws warning on const std::string[]

The name of the pictureThe name of the pictureThe name of the pictureClash 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!










share|improve this question









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














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!










share|improve this question









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












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!










share|improve this question









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






share|improve this question









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.











share|improve this question









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.









share|improve this question




share|improve this question








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












  • 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












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.






share|improve this answer


















  • 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 clearly a bug regardless; there's no "redundant code" here
    – Lightness Races in Orbit
    10 mins ago










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






Mukuma is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















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






























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.






share|improve this answer


















  • 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 clearly a bug regardless; there's no "redundant code" here
    – Lightness Races in Orbit
    10 mins ago














up vote
10
down vote



accepted










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






share|improve this answer


















  • 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 clearly a bug regardless; there's no "redundant code" here
    – Lightness Races in Orbit
    10 mins ago












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.






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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 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












  • 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 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










Mukuma is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















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.













 


draft saved


draft discarded














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













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

One-line joke