不使用 C++ llvm/clang 中设计的异常
LLVM/Clang 被认为是良好的 C++ 代码库。我想知道为什么其中根本不使用 C++ 异常?
内存是使用诸如池之类的东西来管理的,并且使用返回值和代码来报告错误,就像在C中一样。他们甚至将operator new包装为placement new,当没有内存时,它会返回错误而不是异常。
您是否知道为什么 LLVM 哲学不使用 C++ 异常,而大多数书籍都建议使用它们?
LLVM/Clang are considered good C++ code bases. I wonder why C++ exceptions are not used in them at all?
Memory is managed using something like pools, and errors are reported with returned values and codes like in C. They are even wrapping operator new to be placement new that returns error and not exception when no memory.
Do you have idea why LLVM philosophy is not to use C++ exceptions when most books recommend using them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
似乎这不是避免异常的 llvm 哲学。至少我在编码标准(http://llvm.org/docs/CodingStandards.html)中没有发现任何有关异常的信息,所以这取决于开发人员。
为什么它没有被广泛使用?据我所知,llvm 中尚未实现异常支持,因此不可能为 llvm 本身编译 llvm :)。因此,避免例外可能只是历史原因。
Seems it is not a llvm philosophy to avoid exceptions. At least I found nothing about exceptions in the coding standard (http://llvm.org/docs/CodingStandards.html), so it is up to developer.
Why it is not widely used? AFAIK exception support was implemented in llvm not so far, so it was not possible to compile llvm for llvm itself :). So it can be just a historical reason to avoid exceptions.
Chris Lattner 最近在 LLVM 项目 编码标准中澄清了这个问题。
不使用异常和 RTTI 可减少可执行文件的大小并减少开销。 (您可能会争辩说,除非抛出零成本异常,否则没有任何开销。至少,它们实际上将代码分解为更小的基本块并抑制某些类型的代码移动。)
Chris Lattner recently clarified this issue in the LLVM project coding standards.
Not using exceptions and RTTI reduces executable size and reduces overhead. (You might argue that zero-cost exceptions have no overhead unless thrown. At a minimum, they actually break up the code into smaller basic blocks and inhibit some types of code motion.)
编写异常安全的 C++ 代码是一项艰巨的任务。
关闭异常可以加快代码执行速度并减少代码大小。
也许这是相关的。
Writing exception safe c++ code is a difficult task.
Turning off exceptions can speed up code execution and reduce code size.
Maybe this is related.
根据您的编译器和代码,使用异常的程序可能比禁用且不使用异常的等效程序更快或更慢。使用异常的也可能更大或更小。
每个错误处理策略都会产生一些成本,我希望 LLVM 开发人员考虑他们的情况,并发现禁用异常对于 LLVM 来说是更好的决定。
我的建议,也是我从专家那里看到最多的建议,是使用异常来报告失败,除非您有一些具体的、充分的理由不这样做。如果您的原因是性能,那么明智的选择是基于分析。请记住,将使用异常的代码与不使用异常但仍能正确处理错误的代码进行比较至关重要。
Depending on your compiler and your code, a program that uses exceptions can be faster or slower than an equivalent program that disables and doesn't use exceptions. Also the one that uses exceptions may be larger or smaller.
Every error handling strategy incurs some cost, and I expect that the LLVM developers considered their situation and found that disabling exceptions was the better decision for LLVM.
My recommendation, and the recommendation I have most seen from experts, is to use exceptions to report failures unless you have some specific, solid reason not to. If your reason is performance, it would be wise to base your choice on profiling. Remember that it is vital to compare code that uses exceptions to code that doesn't, but still handles errors correctly.
我认为这源于另一条准则: 自由地使用断言
assert
进行处理。我想说
assert
是一个更难的例外:你绝对不能忽略它;)I think this stems from another guideline: Use assert liberally
assert
.I would say that an
assert
is an even harder exception: you definitely cannot ignore it ;)大多数书籍都推荐使用它们吗?我知道大多数关于 C++ 编程的书籍都涵盖了它们,因为它们是该语言的一部分,但我认为我没有见过一本书说更喜欢它们而不是错误代码或其他错误处理方法。事实上,我想说大多数书籍都隐含地不建议使用异常,因为它们没有介绍如何编写良好的异常安全代码。
就 LLVM 是良好的代码且不是基于异常的代码而言,这两个概念在很大程度上是正交的。代码可以干净地编写,无论是否有异常。
Do most books recommend using them? I know most books on C++ programming cover them because they are part of the language, but I don't think I have seen a book that said to prefer them to error codes or other methods of error handling. In fact I would say most books implicitly do not recommend using exceptions because they don't cover how to write good exception safe code.
As far as LLVM being good code and it being not exception based code, the two concepts are largely orthogonal. Code can be cleanly written with or without exceptions.