Menu

cppcheck does not detect inneficient way of remove last character of a std::string

2022-07-03
2022-07-05
  • Dominique Pelle

    Dominique Pelle - 2022-07-03

    cppcheck does not detect this inefficient way of removing last character of a std::string:

    #include <string>
    
    std::string foo(std::string s) {
      // Inefficient way of removing the last character of the string
      // as it creates and moves a temporary string.
      s = s.substr(0, s.size() - 1);
      return s;
    }
    

    It could suggest this instead which does not create a temporary string:

    #include <string>
    
    std::string foo(std::string s) {
      s.pop_back();
      return s;
    }
    
     

    Last edit: Dominique Pelle 2022-07-08
  • Daniel Marjamäki

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.