Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将GCC诊断杂注与C++;模板函数?_C++_Templates_Gcc_Pragma_Diagnostics - Fatal编程技术网

C++ 如何将GCC诊断杂注与C++;模板函数?

C++ 如何将GCC诊断杂注与C++;模板函数?,c++,templates,gcc,pragma,diagnostics,C++,Templates,Gcc,Pragma,Diagnostics,我想使用g++和-Werror,所以我现在必须禁用对我无法控制的第三方库的警告。由提供的解决方案工作得非常好,允许简单地用pragmas包装第三方头的include。不幸的是,在涉及模板的特定设置中,这不再适用于我。我创建了以下示例,说明此方法没有按预期工作: 源文件main.cpp #pragma GCC diagnostic ignored "-Wunused-parameter" #include "hdr.hpp" #pragma GCC diagnostic error "-Wunus

我想使用g++和-Werror,所以我现在必须禁用对我无法控制的第三方库的警告。由提供的解决方案工作得非常好,允许简单地用pragmas包装第三方头的include。不幸的是,在涉及模板的特定设置中,这不再适用于我。我创建了以下示例,说明此方法没有按预期工作:

源文件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;
}
将产生以下编译器错误

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 diagnostic ignored“-Wunused parameter”
放在主函数前面会使编译器静音,而在文件的最后添加
#pragma GCC diagnostic error“-Wunused parameter”
会导致编译器再次产生错误。如何解决这个难题

(注意,关于这个pragma有几十个线程,但我找不到任何线程
(这涉及到这样的设置)

表示不打算使用参数的通常方法是不给它命名:

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

int mystandardfunc(int /* i */) 
{ return 53; } 
模板
int mytemplatefunc(T/*T*/)
{return 42;}
int mystandardfunc(int/*i*/)
{返回53;}

问题在于,模板的实例化是在您使用它时编译的,而不是在编译器在头文件中解析它时编译的,因此它不会发出警告,直到它用int替换T并将其作为pragma静默上下文之外的常规函数进行解析。

从提供的链接,“GCC跟踪每个pragma的位置,并根据源文件中该点的状态发布诊断。因此,
#pragma
出现在一行之后不会影响该行引起的诊断。“因此,如果您仍然遇到问题,即使您只是在代码末尾重新打开错误。。。您是否尝试过将第二个
#pragma
作为
诊断警告
而不是
诊断错误
?如果它仍然将其报告为错误而不是警告,那么您可能希望检查头文件中的
#pragma
s.Nope,然后它将报告为警告。但这不符合我的要求。这样做的全部思想是使用错误而不是警告(-Werror),这样我将收到编译失败的通知,而不必手动扫描编译器输出的多个页面(如果我必须区分我实际上可以处理的警告和我必须忽略的警告,这将是很困难的)。我认为HDR.HPP超出了我的控制范围(想象它是第三方)。似乎与这个问题有关。我使用的是GCC4.4.5。这似乎不支持在比文件级别更精细的范围内使用这种pragma。在进一步的测试中,我去掉了头球,玩了-Wuninitialized,它的行为也很奇怪。是的,这很有效!事实上,我应该在我的描述中包含标题实际上超出了我的控制范围,因此我无法修复标题,而必须在代码中包含该标题的部分使警告静音。
template<typename T> 
int mytemplatefunc(T /* t */) 
{ return 42; } 

int mystandardfunc(int /* i */) 
{ return 53; }