Menu

False positive constVariable on std::map

2023-03-08
2023-03-14
  • Pierre DEL PERUGIA

    Hello, the following code:

    std::vector< std::map< std::string, std::string > > rows;
    
    rows.push_back( { { "b", "11" }, { "c", "12" } } );
    rows.push_back( { { "b", "21" }, { "c", "22" } } );
    
     for ( auto & row : rows )
         std::cout << row[ "b" ] + "\n";
    

    produces on the for line a warning:

    style: Variable 'row' can be declared as reference to const [constVariable]

    but row cannot be declared const because the map operator [] is not const.

    Tested using Cppcheck 2.10 dev.

    Thanks

     

    Last edit: Pierre DEL PERUGIA 2023-03-08
  • CHR

    CHR - 2023-03-08

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

     
  • Pierre DEL PERUGIA

    Hi, thanks.

    I check with today's version (https://github.com/danmar/cppcheck/commit/61e8b84578263d018c9398efe9b4137e513cda3e) which includes Fix #11607 FP constVariable with auto and std::map (#4877) and it seems the problem is still there. Warning is reported, even on the function:

    void f(std::vector<std::map<int, int>>& v) {
         for (auto& m : v)
             std::cout << m[0];
    }
    
     
  • CHR

    CHR - 2023-03-13

    Not sure what's going on, both samples are clean with head on my end.

     
  • Pierre DEL PERUGIA

    Sorry, my mistake: bad deploy of the new bin.
    Thanks a lot!

     
    👍
    1

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.