- What language is this for?
C++ - Which rule?
cpp:S5425 - Why do you believe it’s a false-positive/false-negative?
It is best practice (or at least not a code style issue) to take lambdas to invoke for generic algorithms using universal references. See for example: How To Pass Lambda Functions in C++ (By Value, By L-Value Reference, By Universal Reference) - Johannes Unterguggenberger
So in this case, an exception needs to be made - std::forward() does not need to be called on the parameter, even though it is a universal reference. It seems that you already make an exception for the easy case (direct invocation), but not when operator() is called explicitly. I need to call operator() explicitly in order to specify template arguments for a templated operator().
- Are you using
- SonarQube - which version?
Enterprise Edition Version 9.9 (build 65466)
- SonarQube - which version?
- How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
#include <iostream>
#include <typeinfo>
template<typename F>
void Invoke(F&& f) // No warning
{
f();
}
template<typename F>
void InvokeExplicit(F&& f) // cpp:S5425 warning
{
f.operator()();
}
template<typename F>
void InvokeTemplated(F&& f) // cpp:S5425 warning
{
f.template operator()<int>();
}
int main(){
Invoke([](){std::cout << "hello1\n"; });
Invoke([](){std::cout << "hello2\n"; });
InvokeTemplated([]<typename T>() {std::cout << typeid(T).name() << "\n";});
}