6

I would like to use g++ and -Werror, so I have now to disable warnings for 3rd-party libraries I have no control of. The solution provided by http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html works very well, allowing simply to wrap the includes of 3rd party headers with pragmas. Unfortunately, that did no longer work for me in a certain setup where templates are involved. I created the following minimal example of where this approach did not work as expected:

Source file main.cpp

#pragma GCC diagnostic ignored "-Wunused-parameter"
#include "hdr.hpp"
#pragma GCC diagnostic error "-Wunused-parameter"
int main() {
    return mytemplatefunc(2) + mystandardfunc(3); // will print ONLY ONE warning
}

and the header hdr.hpp

template<typename T>
int mytemplatefunc(T t) {
    return 42;
}
int mystandardfunc(int i) {
    return 53;
}

compiled using Makefile

CPPFLAGS+=-Wunused-parameter -Werror
main: main.cpp

will produce the following compiler error

g++  -Wunused-parameter -Werror   main.cpp   -o main
In file included from main.cpp:3:
hdr.hpp: In instantiation of ‘int mytemplatefunc(T) [with T = int]’:
main.cpp:29:   instantiated from here
hdr.hpp:2: error: unused parameter ‘t’
make: *** [main] Error 1
shell returned 2

Note that explicit instantiation in main.cpp directly after including the header did not work, and wrapping the call to the template function in main.cpp did not work either. What was puzzling that putting #pragma GCC diagnostic ignored "-Wunused-parameter" in front of the main function silenced the compiler, whilst then adding #pragma GCC diagnostic error "-Wunused-parameter" at the very end of the file caused the compiler to produce the error again. How to solve this puzzle?

(Note, there are dozens of threads about this pragma, but I could not find anyone that involved such a setup)

Julius
  • 93
  • 2
  • 6
  • From the link provided, "GCC keeps track of the location of each pragma, and issues diagnostics according to the state as of that point in the source file. Thus, `#pragma`s occurring after a line do not affect diagnostics caused by that line." So if you still get problems even if you're only turning the error back on at the end of the code... have you tried doing that second `#pragma` as `diagnostic warning` rather than `diagnostic error`? If it still reports it as an error rather than a warning, then you might want to check your header file for `#pragma`s. – JAB Jun 03 '11 at 13:46
  • Nope, then it will report as a warning. But that does not meet my requiremens. The whole idea of this is to have errors instead of warnings (-Werror) such that I will be notified by a compilation failure rather than having to scan multiple pages of compiler output manually (which would be difficult if I then have to distinguish between warnings I can actually do something about, and warnings I have to ignore). I consider hdr.hpp to be out of my control (imagine it's 3rd-party). – Julius Jun 05 '11 at 05:14
  • http://www.spinics.net/lists/gcchelp/msg30702.html seems to relate to this problem. I am using gcc 4.4.5. and that seems not to support such pragma use at a finer scope than file-level. In a further test, I did away with the header and played with -Wuninitialized and it behaves also strangely. – Julius Jun 14 '11 at 21:29

2 Answers2

5

The issue is that the instantiation of the template is compiled when you use it, not when it is parsed by the compiler in the header file so it will not issue the warning until it replaces T by int and parses it as a regular function outside the context of the pragma silencing.

David
  • 3,324
  • 2
  • 27
  • 31
2

The usual way to indicate that you don't intend to use a parameter is to not give it a name:

template<typename T> 
int mytemplatefunc(T /* t */) 
{ return 42; } 

int mystandardfunc(int /* i */) 
{ return 53; } 
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Yes, that works! Actually, I should have included in my description that the header is actually out of my control, so I cannot fix the header and have to silence the warnings instead in the part of code where I am including that header. – Julius Jun 05 '11 at 05:03