Menu

False positive: constVariableReference

2023-07-31
2023-08-04
  • Samuel Poláček

    Cppcheck 2.12 dev

    cppcheck --enable=all test.cpp
    
    #include <map>
    
    std::map<int, int>& get_map_ref()
    {
        static std::map<int, int> map;
        return map;
    }
    
    int main()
    {
        /* const */ auto& map = get_map_ref();
        auto const it = map.find(42);
        int* i_ptr = it != std::end(map) ? &it->second : nullptr;
        return i_ptr ? *i_ptr : 0;
    }
    

    test.cpp:11:23: style: Variable 'map' can be declared as reference to const [constVariableReference]
    / const / auto& map = get_map_ref();

    The variable can't be const as that would change the type of &it->second to const int*, which cannot be assigned to int* i_ptr.

     

    Last edit: Samuel Poláček 2023-07-31
  • CHR

    CHR - 2023-08-04
     

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.