简体   繁体   English

在llvm / clang中没有按设计使用C ++异常

[英]Not using C++ exceptions by design, in llvm/clang

llvm/clang are considered good C++ code bases. llvm / clang被认为是很好的C ++代码库。 I wonder why C++ exceptions arenot used in them at all? 我想知道为什么C ++异常根本不用于它们?

Memory is managed using something like pools, and erros are reported with returnd values and codes like in C. They even wrapping operator new to be placement new that returns error and not exception when no memory. 使用类似池的内容管理内存,并使用返回值和C中的代码报告错误。它们甚至将operator new包装为placement new,返回错误,而没有内存时也不例外。

Do you have idea why llvm philosophy is not to use C++ exceptions when most books recommend using them? 您是否知道为什么llvm哲学在大多数书籍推荐使用时不会使用C ++异常?

Chris Lattner recently clarified this issue in the LLVM project coding standards . Chris Lattner最近在LLVM项目编码标准中澄清了这个问题。

Not using exceptions and RTTI reduces executable size and reduces overhead. 不使用异常和RTTI减少了可执行文件的大小并减少了开销。 (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.) (你可能会争辩说,零成本异常除非被抛出,否则没有开销。至少,它们实际上将代码分解为更小的基本块并禁止某些类型的代码运动。)

Writing exception safe c++ code is a difficult task. 编写异常安全的c ++代码是一项艰巨的任务。

Turning off exceptions can speed up code execution and reduce code size . 关闭异常可以加快代码执行速度并减少代码大小

Maybe this is related. 也许这是相关的。

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. 每个错误处理策略都会产生一些成本,我希望LLVM开发人员考虑他们的情况,并发现禁用异常是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 . 请记住,将使用异常的代码与不能代码但仍能正确处理错误的代码进行比较至关重要。

I think this stems from another guideline: Use assert liberally 我认为这源于另一个准则: 自由地使用断言

  • Normal error conditions are treated using error codes. 使用错误代码处理正常错误条件。
  • Exceptional error conditions are treated via assert . 通过assert处理异常错误条件。

I would say that an assert is an even harder exception: you definitely cannot ignore it ;) 我会说assert是一个更难的例外:你绝对不能忽视它;)

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. 我知道大多数关于C ++编程的书籍都涵盖了它们,因为它们是语言的一部分,但我认为我没有看过一本书,说它更喜欢错误代码或其他错误处理方法。 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. 至于LLVM是良好的代码并且它不是基于异常的代码,这两个概念在很大程度上是正交的。 Code can be cleanly written with or without exceptions. 代码可以干净利落地编写,有或没有例外。

Seems it is not a llvm philosophy to avoid exceptions. 似乎避免例外不是一种llvm哲学。 At least I found nothing about exceptions in the coding standard (http://llvm.org/docs/CodingStandards.html), so it is up to developer. 至少我没有发现编码标准中的异常(http://llvm.org/docs/CodingStandards.html),所以这取决于开发人员。

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 :). 到目前为止,在llvm中实现了AFAIK异常支持,因此无法为llvm本身编译llvm :)。 So it can be just a historical reason to avoid exceptions. 因此,这可能只是避免例外的历史原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM