I see a false positive running cppcheck 2.22.0 against the following code.
Note the parameter cannot be declared as reference to const because its job is to match both const and mutable references, and return a reference to a member which retains the const-ness of the first argument (similar to the 'deduce this' pattern). The static asserts demonstrate the intended use cases.
Note also that the false positive goes away if the concept is inlined as a template requires clause on the function itself (first argument becomes T&).
#include<concepts>#include<type_traits>// Reproduce with:// cppcheck --enable=all --inconclusive --check-level=exhaustive constParameterReference.cppenumclassplayer{black,white};structcaptures{unsignedintcaptured_by_black{0};unsignedintcaptured_by_white{0};};template<typenameT>conceptcaptures_type=std::same_as<std::remove_cvref_t<T>,captures>;constexprauto&captured_pieces_for(captures_typeauto&captures,playercurrent_player){returncurrent_player==player::black?captures.captured_by_black:captures.captured_by_white;}/// static assertions show intended behaviour for captured_pieces_for with different cv-qualifiers.static_assert(std::same_as<decltype(captured_pieces_for(std::declval<captures&>(),player::black)),unsignedint&>);static_assert(std::same_as<decltype(captured_pieces_for(std::declval<constcaptures&>(),player::black)),constunsignedint&>);intmain(){capturescaptures{};auto&total=captured_pieces_for(captures,player::black);++total;returncaptures.captured_by_black==1?0:1;}
The error output:
constParameterReference.cpp:21:57: style: Parameter 'captures' can be declared as reference to const [constParameterReference]
constexpr auto& captured_pieces_for(captures_type auto& captures, player current_player)
^
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I see a false positive running cppcheck 2.22.0 against the following code.
Note the parameter cannot be declared as reference to const because its job is to match both const and mutable references, and return a reference to a member which retains the const-ness of the first argument (similar to the 'deduce this' pattern). The static asserts demonstrate the intended use cases.
Note also that the false positive goes away if the concept is inlined as a template requires clause on the function itself (first argument becomes T&).
The error output:
Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/15087