Menu

False positive when converting std::string_view to std::string

2022-11-04
2022-11-07
  • Mark Bourgeault

    Mark Bourgeault - 2022-11-04

    cppcheck 2.8 triggers a danglingTemporaryLifetime error. I believe this is a false positive.

    #include <string_view>
    #include <string>
    #include <vector>
    #include <iostream>
    
    std::string convert(std::string_view sv) { return std::string{sv}; }
    
    int main()
    {
        std::vector<std::string> v;
        v.push_back(convert("foo"));
        for (const std::string& str : v)
            std::cout << str << '\n';
        return 0;
    }
    
     
  • CHR

    CHR - 2022-11-07

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/11374

     

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.