ANTLR3 C 目标与 C++例外情况

发布于 2024-09-11 22:00:06 字数 547 浏览 9 评论 0原文

我对 ANTLR 2 的 C++ 目标有一些经验,但由于担心异常安全,我一直犹豫是否要在 ANTLR 3 上花费太多时间。

遗憾的是,ANTLR 3 只有一个 C 目标,可以生成“C++ 兼容”的 C。基于以下内容,这似乎包括 C++ 异常安全:

你也许可以小心地使用[例外], 但正如你所指出的,你必须 小心记忆。运行时 追踪其所有正常记忆 分配,只要你关闭 你应该正确地“课程” 一般都可以。但是,您应该 确保抛出异常 不绕过正常规则干净 上,例如重置错误和 回溯标志等等。

ANTLR-interest,大约 2009 年

有人吗有使用 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.

(ANTLR-interest, circa 2009)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咋地 2024-09-18 22:00:06

我没有任何 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文