是否有其他方法可以抑制 xtree 中无法访问的代码的警告?
当将 std::map 与使用简单、非抛出、复制构造函数的类型一起使用时,对于 xtree 中无法访问的代码,会抛出编译器警告/错误(警告级别 4,发布模式)。这是因为 std::map 中有一个 try-catch,它有助于在发生异常时维护树的状态,但编译器发现,如果存储的元素不存在,则永远不会调用 catch 语句。扔。可以使用 .cpp 文件顶部的以下行轻松抑制这些警告:
#pragma warning(push)
#pragma warning(disable:4702)
#include <xtree>
#pragma warning(pop)
是否有一种方法可以绕过此警告/错误,而无需更改警告级别、构建调试、抑制警告或在地图?标准库有计划改变这一点吗?
更新:
也许它是特定于编译器的。我用的是vc7。 错误如下:
c:\program files\microsoft Visual Studio .net 2003\vc7\include\xtree(1116):错误 C2220:警告被视为错误 - 未生成“对象”文件
c:\program files\microsoft Visual Studio .net 2003\vc7\include\xtree(1116) : warning C4702: unreachable code
显然,xtree 由 std::map 使用。
When using the std::map with types that use trivial, non-throwing, copy constructors, a compiler warning/error is thrown (warning level 4, release mode) for unreachable code in xtree. This is because the std::map has a try-catch in it that helps maintain the state of the tree in the case of an exception, but the compiler figures out that the catch statement will never be called if the stored elements don't throw. These warnings can be easily suppressed with the following lines at the top of the .cpp file:
#pragma warning(push)
#pragma warning(disable:4702)
#include <xtree>
#pragma warning(pop)
Is there a way to bypass this warning/error without changing the warning level, building in debug, suppressing the warning, or using a different type in the map? Is there plans to change this in the standard library?
Update:
Maybe it is compiler specific. I am using vc7.
The error is below:
c:\program files\microsoft visual studio .net 2003\vc7\include\xtree(1116) : error C2220: warning treated as error - no 'object' file generated
c:\program files\microsoft visual studio .net 2003\vc7\include\xtree(1116) : warning C4702: unreachable code
Apparently the xtree is used by the std::map.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,看起来
xtree
是VC7中map
底层实现的一部分,因此没有太多可以采取的措施来缓解它。看起来这是标准库中的一个错误。是否可以使用较新的编译器?我相当确定可以免费下载您可以使用的更新版本的编译器,也许他们已经解决了这个问题。
如果这不是一个选项,可能最好的解决方案是将
map
的包含内容包装到您自己的私有标头中,并添加注释和#pragma
+include您已经发现的
行(除了包含map
之外)。这样您就可以隐藏正常使用的解决方法。Unfortunately it looks like
xtree
is part of the underlying implementation ofmap
in VC7, and as such there isn't much that can be done to mitigate it. It looks like it's a bug in the standard library.Is it a possibility to use a newer compiler? I'm fairly sure there are free downloads of more recent versions of the compiler you could use, and perhaps they've fixed this issue.
If that's not an option, probably the best solution is to wrap the include of
map
into your own private header, complete with a comment and the#pragma
+include <xtree>
lines you already discovered (in addition to an include ofmap
. This way you hide the workaround from normal use.