News for hackers. Stuff that matters.
Re:LLVM Programmer’s Manual by quotemstr on Friday July 22, 2016 @04:23AM (#12142362)
C++ projects banning exceptions is a massive blunder. We should not disable important aspects of the language that provide useful semantic features to satisfy the anti-bloat superstitions of a few who remember bad C++ compilers of the 1990s. Nobody feels the need to create an exception-less variant of Java or Python, after all.

A ban on C++ exceptions is a huge red flag for me. A project has to be really special if I'm going to contribute to it after it's crippled the language.

Re:LLVM Programmer’s Manual by gakada on Friday July 22, 2016 @07:53AM (#12143000)
Exceptions in C++ are rotten.

    void read_file_by_lines(const char* path, void (*callback)(const char* line)) {
        FILE* file = fopen(path, "r");
        
        size_t length = 0;
        char* line = NULL;
        
        while (-1 != getline(&line, &length, file)) {
            callback(line);
        }
        
        free(line);
        fclose(file);
    }
This C function should be compatible with C++. If exceptions are off, it is. If exceptions are on then it's dangerous because if an exception is thrown through callback, then the file handle and line buffer will leak.

C++ exceptions are predicated on the assumption that all resources are managed using RAII. In the real world, this isn't true, especially considering that C doesn't even have RAII.

Python programmers (including me) love their exceptions, because Python is a dynamic language, and exposes system resources as reference counted objects.

Re:LLVM Programmer’s Manual by pif on Friday July 22, 2016 @08:37AM (#12143153)
Your example is wrong. This function is compatible with C++ as soon as you document it as not accepting exception-throwing callbacks.
Re:LLVM Programmer’s Manual by gakada on Friday July 22, 2016 @10:10AM (#12143579)
I'm sorry, I can't capture such a complicated issue with a pithy example. You will have to settle for my illustrative one.

The most general definition of the restriction is that C++ code cannot throw exceptions through exception unsafe code. How do you know if your C++ function was called from exception unsafe code? You can't!

So it's only ever safe to throw an exception when you are expecting it to be caught immediately i.e. you can use exceptions as glorified error codes, but not much else.

In Django I can throw a Http404 exception from deep inside a view and the server will come up with a 404 response. I can assert and the client will get a 500 error but the server won't go down. This is the real power of exceptions, and God help anybody who tries to do it in C++.

Re:LLVM Programmer’s Manual by pjmlp on Friday July 22, 2016 @02:58PM (#12145861)
That is exactly one of the pet peeves I have with C++ and although I like the language I am happy to earn my money using other languages.

In the enterprise context you can hardly count on proper documentation, specially in projects with high attrition of consultants where each one comes, implements something and departures to the next gig.

Not to count the commercial libraries distributed in binary only form.

So one never knows if the code is exception safe, don't work with exceptions or is exception happy.

Re:LLVM Programmer’s Manual by wyldfire on Friday July 22, 2016 @08:58AM (#12143221)
Your example strikes me as "exceptions in C++ are [yet another] high-discipline task to manage."

I think there are examples related to inter-library exceptions that might be much more subtle than this one, but I've never understood the subtlety.

Re:LLVM Programmer’s Manual by coherentpony on Friday July 22, 2016 @11:59AM (#12144345)
Is the fault the exceptions or is it that the person that wrote this C++ function didn't use objects to manage their resources?
Re:LLVM Programmer’s Manual by pjmlp on Friday July 22, 2016 @08:29AM (#12143122)
This is the real issue, so many C++ features are hampered due to the drop-in compatibility with C code.
Re:LLVM Programmer’s Manual by perspectivep on Friday July 22, 2016 @10:29AM (#12143708)
The solution is to just make RAII wrappers for that stuff.
Re:LLVM Programmer’s Manual by bigcheesegs on Friday July 22, 2016 @05:31AM (#12142563)
The llvm-project is a compiler. Its developers are well aware of the true cost of exceptions.
Re:LLVM Programmer’s Manual by barrkel on Friday July 22, 2016 @06:36AM (#12142753)
Actually, compilers typically don't use exceptions because they don't usually abort on the first instance of a problem. Compilation has historically been an lengthy batch job, and thus reporting many errors to be fixed between iterations improves user productivity.
Re:LLVM Programmer’s Manual by strcat on Saturday July 23, 2016 @01:10AM (#12148445)
LLVM doesn't take shortcuts like that. It's designed as a set of reusable libraries for use in all kinds of tooling. In many cases it even has to continue going after detecting errors and recording a representation of them, so that it can still produce useful information. That's an important feature for lots of tooling.
Re:LLVM Programmer’s Manual by favorited on Friday July 22, 2016 @02:01PM (#12145389)
I think the point was that LLVM compiler infrastructure has a first-class C++ font-end. They know the ins-and-outs of language features because they are implementing standards-compliant compilers for said language.
Re:LLVM Programmer’s Manual by barrkel on Friday July 22, 2016 @05:07PM (#12146779)
Most self-hosted compilers are written in languages ill-suited to compiler writing. The choice of features used is often a compromise, rather than best practice for either good compiler design or good target language use.

C++ users in particular have strong concerns about things like RTTI and the overhead of exceptions not thrown. The cost of making unthrown exceptions low (near zero) is that throwing exceptions is much more expensive. This turns into a feedback loop, where C++ programs end up performing better if they never throw any exceptions because of the tradeoff.

This feeds into self-hosted compilers. Thing is, certain compiler design techniques can work well with cheap unwinding: constraint solving (more common in languages with better type systems than C++), forward-looking grammar assertions with speculative parsing, IDE-integrated code completion (implement the lexer to add the cursor position as a special token, and jump out of the parser when it's found, where all the context is available to pass along). But C++'s peculiar tradeoffs may make using things like exceptions less than ideal to use for this purpose, not because exceptions are the wrong tool, but because most C++ programs are not compilers and aren't tuned for it.

And OTOH, compilers written in low-level languages like C++ often twist themselves into knots to simulate things like tree pattern matching, multiple dispatch, destructuring binds, etc. Sometimes they even give up and write code generating tools.

Re:LLVM Programmer’s Manual by pjmlp on Friday July 22, 2016 @08:35AM (#12143145)
I agree with you, however Bjarne wanted to sneak C++ into C code, given his experience being forced to use C (there are a few interviews where he mentions this).

So by having exceptions, with code that only knows about C semantics but was compiled with C++ mode, you open the door to all sorts of nasty surprises.

So exceptions can cause resource leaks and other issues like being thrown in the middle of a UNIX signal.

Which leads to the situation that sadly many library writers avoid them.

I still enjoy using C++, but the best place for it is as infrastructure language for the bottom layer when performance requires it, then using something else on the other layers.