0

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.

tmaric
  • 5,347
  • 4
  • 42
  • 75
  • 2
    C++ allows you to omit argument names. If you're not using the arguments then don't provide symbols for them. Either that or use [the `[[maybe_unused]]` attribute](https://en.cppreference.com/w/cpp/language/attributes/maybe_unused). – Some programmer dude Feb 15 '19 at 11:42

1 Answers1

2

just remove the name of the parameters :

static bool applies(T1 const& , T2 const& )
{
    return true;
}
bruno
  • 32,421
  • 7
  • 25
  • 37