Let's say I have an algorithm that performs an inexpensive test on its arguments before doing an expensive calculation:
struct no_test
{
template<typename T1, typename T2>
static bool applies(T1 const& t1, T2 const& t2)
{
return true;
}
};
struct some_test
{
template<typename T1, typename T2>
static bool applies(T1 const& t1, T2 const& t2)
{
return t1 < t2;
}
};
template<typename T1, typename T2, typename Test = no_test>
void some_algorithm(T1 const& t1, T2 const& t2)
{
if (Test::applies(t1, t2))
{
// Do some work.
}
}
int main()
{
some_algorithm(1.0, 2);
}
If this code is compiled with -Wunused-parameter
with gcc
, a warning is generated:
main.cpp:4:35: warning: unused parameter ‘t1’ [-Wunused-parameter]
static bool applies(T1 const& t1, T2 const& t2)
~~~~~~~~~~^~
main.cpp:4:49: warning: unused parameter ‘t2’ [-Wunused-parameter]
static bool applies(T1 const& t1, T2 const& t2)
But in this case, no_test
is not using t1
and t2
on purpose, because if I don't want some_algorithm
to perform the test, no_test::applies
can be used because it always returns true.
For the rest of my code, I would like to output warnings if function parameters are not used.