3

I recently tested to build my code with clang instead of gcc. It fails, since I use -Werror because of an unused parameter in a template function. With gcc this does not happen.

Here is a small example:

template <typename REAL> int f(int a){return 42;}

int g(int a){return 42;}

Compiling it with clang give me the result I was hoping for:

$clang -c UnusedParam.cpp  -Wunused-parameter
>UnusedParam.cpp:1:36: warning: unused parameter 'a' [-Wunused-parameter]
> template <typename REAL> int f(int a){return 42;}
>                                   ^
>UnusedParam.cpp:3:11: warning: unused parameter 'a' [-Wunused-parameter]
> int g(int a){return 42;}

Gcc does only report the unused parameter in the second function.

$gcc -c UnusedParam.cpp  -Wunused-parameter
>UnusedParam.cpp:3:5: warning: unused parameter ‘a’ [-Wunused-parameter]
> int g(int a){return 42;}

Is there a way to enforce a similar behavior? I would like gcc to generate the unused parameter warning as well.

Compiler:

  • Clang: Version 3.3 (branches/release_33 183898)
  • Gcc: Version 4.8.1 20130909
tgmath
  • 12,813
  • 2
  • 16
  • 24

1 Answers1

6

Thanks @DieterLücking, your comment helped me a lot. Gcc seems to generate unused parameter warings only when the template function is instantiated. Which seems to be a valid decision for this kind of warning, even so I would prefer Clang's warning.

Here is a version where Gcc is complaining about the unused parameter:

template <typename REAL> int f(int a){return 42;}

int h(){return f<int>(3);}

Gcc Warning:

$ gcc -c UnusedParam.cpp  -Wunused-parameter
>UnusedParam.cpp: In instantiation of ‘int f(int) [with REAL = int]’:
>UnusedParam.cpp:4:24:   required from here
>UnusedParam.cpp:1:30: warning: unused parameter ‘a’ [-Wunused-parameter]
 template <typename REAL> int f(int a){return 42;}
tgmath
  • 12,813
  • 2
  • 16
  • 24