zaharpopov
zaharpopov

Reputation: 17262

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

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?

Upvotes: 29

Views: 11240

Answers (6)

ohmantics
ohmantics

Reputation: 1819

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.)

Upvotes: 23

Todd Greer
Todd Greer

Reputation: 116

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.

Upvotes: 5

Matthieu M.
Matthieu M.

Reputation: 299760

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.

I would say that an assert is an even harder exception: you definitely cannot ignore it ;)

Upvotes: 2

rotoglup
rotoglup

Reputation: 5238

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.

Upvotes: 7

Yuras
Yuras

Reputation: 13876

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.

Upvotes: 0

stonemetal
stonemetal

Reputation: 6208

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.

Upvotes: 0

Related Questions