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.
const
&it->second
const int*
int* i_ptr
Tracked here: https://trac.cppcheck.net/ticket/11857
Log in to post a comment.
Cppcheck 2.12 dev
The variable can't be
const
as that would change the type of&it->second
toconst int*
, which cannot be assigned toint* i_ptr
.Last edit: Samuel Poláček 2023-07-31
Tracked here: https://trac.cppcheck.net/ticket/11857