如何将 GCC 诊断编译指示与 C++ 结合使用模板函数?
我想使用 g++ 和 -Werror,所以我现在必须禁用我无法控制的第 3 方库的警告。 http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html<提供的解决方案/a> 工作得很好,允许简单地用编译指示包装第 3 方标头的包含内容。不幸的是,在涉及模板的特定设置中,这不再对我有用。我创建了以下最小示例,说明此方法未按预期工作:
源文件 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
}
标头 hdr.hpp
template<typename T>
int mytemplatefunc(T t) {
return 42;
}
int mystandardfunc(int i) {
return 53;
}
和使用 Makefile 编译的
CPPFLAGS+=-Wunused-parameter -Werror
main: main.cpp
将产生以下编译器错误
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
请注意,在包含标头后直接在 main.cpp 中显式实例化不起作用,并且在 main.cpp 中包装对模板函数的调用也不起作用。令人费解的是,将 #pragma GCC 诊断忽略“-Wunused-parameter” 放在主函数前面会使编译器保持沉默,然后添加 #pragma GCC 诊断错误“-Wunused-parameter”文件最末尾的 导致编译器再次产生错误。如何解决这个难题?
(注意,有几十个关于这个编译指示的线程,但我找不到任何人 涉及这样的设置)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是模板的实例化是在您使用它时编译的,而不是在头文件中由编译器解析时编译的,因此在将 T 替换为 int 并将其解析为外部常规函数之前,它不会发出警告。杂注沉默的上下文。
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.
表明您不打算使用参数的常用方法是不给它命名:
The usual way to indicate that you don't intend to use a parameter is to not give it a name: