ANTLR3 C 目标与 C++例外情况
我对 ANTLR 2 的 C++ 目标有一些经验,但由于担心异常安全,我一直犹豫是否要在 ANTLR 3 上花费太多时间。
遗憾的是,ANTLR 3 只有一个 C 目标,可以生成“C++ 兼容”的 C。基于以下内容,这似乎不包括 C++ 异常安全:
你也许可以小心地使用[例外], 但正如你所指出的,你必须 小心记忆。运行时 追踪其所有正常记忆 分配,只要你关闭 你应该正确地“课程” 一般都可以。但是,您应该 确保抛出异常 不绕过正常规则干净 上,例如重置错误和 回溯标志等等。
有人吗有使用 ANTLR C 目标和(高级)C++ 的经验吗?是否可以安全地抛出异常?我必须编写哪些额外代码(如果有)才能确保安全?
I have some experience with ANTLR 2's C++ target, but have been hesitant to spend much time on ANTLR 3 because of my fears about exception safety.
Sadly, ANTLR 3 only has a C target which produces C which is "C++ compatible." This does not seem to include C++ exception safety, based on the following:
You can probably use [exceptions] carefully,
but as you point out, you have to be
careful with memory. The runtime
tracks all its normal memory
allocations so as long as you close
the 'classes' correctly you should
generally be OK. However, you should
make sure that throwing exceptions
does not bypass the normal rule clean
up, such as resetting error and
backtracking flags and so on.
Does anyone have any experience using the ANTLR C target with (advanced) C++? Is it possible to safely throw exceptions? What extra code (if any) do I have to write to make it safe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有任何 ANTLR 经验(遗憾的是......),但是没有办法让 C 代码在异常情况下工作。我建议您参阅更有效的 C++,第 9 项:“使用析构函数防止资源泄漏”
这个想法是,如果在清理过程中引发异常,您将无法了解哪些内容已被删除(),哪些内容尚未删除,并且您的软件会泄漏内存。如果您使用 auto_ptr/scroped_ptr,则不必担心这一点,因为编译器会自行处理它。
但这种习惯用法仅适用于 C++,C 在设计时并未考虑到例外情况。
I don't have any ANTLR experience (sadly...), but there is NO way to make C code work with exceptions around. I refer you to More Effective C++, item 9 : "Use destructors to prevent resource leaks"
The idea is that if an exception is thrown during cleanup, you have no information on what is already delete()ed an what is not, and your software will leak memory. If you use auto_ptr/scroped_ptr, ou don't have to worry about this, since the compiler will deal with it itself.
But this idiom is C++-only, C was not designed with exceptions in mind.