By analyzing some code using SonarLint, I saw it complaining about the second parameter (Args... args
) of the function below. It says:
"std::forward" should only be called on a forwarding reference.
Here is my code:
#include <iostream>
#include <chrono>
#include <utility>
#include <functional>
template < typename Time = std::chrono::microseconds,
typename Clock = std::chrono::steady_clock >
struct FunctionTimer
{
template < typename F, typename... Args >
static Time duration( F&& f, Args... args )
{
auto start { Clock::now( ) };
// here it says that I shouldn't use std::forward because
// `args` is not a forwarding reference
std::invoke( std::forward<F>( f ), std::forward<Args>( args )... );
auto end { Clock::now( ) };
return std::chrono::duration_cast<Time>( end - start );
}
};
void test_func( const int param )
{
std::cout << param << " Testing...\n";
}
int main( )
{
const auto time { FunctionTimer<>::duration( test_func, 5 ) };
std::cout << std::chrono::duration<double, std::milli>( time ).count( ) << '\n';
}
So is this a misleading warning? If not then how should I fix it and make it right?