Live data from Hacker News

Is C++ Doomed?

tednesday.wordpress.com

151–160 of 168 posts

Re: Is C++ Doomed?

#151
post #45

I am very confused. Some of these ramblings are just plain wrong, like > But exceptions come at a performance cost. No they don't. An exception that isn't thrown costs nothing. Also > Let’s say I want to know if a constructor failed. I have two options, one is to pass in an in-out parameter, the other is to the throw an exception. Using a factory function instead is a third option that avoids this. Besides, this is a…

> No they don't. An exception that isn't thrown costs nothing.

Wait whaaat?

When every expression can throw anything, the compiler and the user both loose the ability to reason about code locally. They change any function from returning one value of one type, to returning any value of any type.

This:

- has a performance cost: it breaks lots of compiler optimizations that require knowing where and what can functions return.

- has a compile-time cost: compiler needs to generate code for landing pads all over the place, this code must be optimized, etc.

- has a code-size cost: the landing pads must be generated in the binary,

- has a usability cost: users can't reason about their programs if they can't reason about where functions can return and what they can return

- has a teachability cost: users have to be taught _a lot_ about how to use exceptions properly because of their cost. Herb Sutter has a whole damn series of books, a series of damn books, about how to use exceptions properly and avoid their pitfalls. When you need a series of books to explain 1 single feature of your programming language, that feature is _wrong_. And writing and reading these books has a huge cost.

Exceptions are as free as Google or Facebook. You can use them for free as long as you gift them your soul so that they can sell it for profit.

Re: Is C++ Doomed?

#152
post #94
post #66

Earlier quoted context omitted.

> The author seems to look for something like Go instead But in Go you also have to write exception safe code, something the author complained about. Before someone says "but you're not supposed to throw or catch in Go": the std http server swallows exceptions thrown in handlers, and fmt.Print does for String callbacks too. And before someone says Go doesn't have exceptions: It does, 100%, in all but name. I actually…

> And before someone says Go doesn't have exceptions: It does, 100%, in all but name. But here the name actually matters. They're called "panic"s, not exceptions. When you panic, you signal a fatal error that should ideally terminate the program. You shouldn't care about closing files or freeing memory, because well-written Go does not panic, and if it does, execution will end very soon. A panic in a String() method…

> But here the name actually matters. They're called "panic"s, not exceptions.

But crucially that doesn't at all affect what they do, how they do it, or what it means for programmers needing to write code.

Even if you never write "panic", or never write "recover", your code still needs to be exception safe.

> When you panic, you signal a fatal error that should ideally terminate the program

The "should ideally" is the flaw in this plan. If your code could EVER be called within an HTTP handler, or from a String() function, then you NEED to write it exception safe.

This code is a time bomb:

    […]
    foo.Lock()
    something()
    foo.Unlock()
    […]
To function correctly it must be:

    […]
    func() {
      foo.Lock()
      defer foo.Unlock()
      something()
    }()
    […]
> You shouldn't care about closing files or freeing memory

But I do care about deleting tempfiles, unlocking mutexes, closing channels, etc…, because you do need to do that.

And this is the Go authors themselves showing off panics, saying "there's a bug" if the file isn't closed:

https://go.dev/blog/defer-panic-and-recover

Go authors explicitly say you should NOT count on finalizers for correct behaviour. So no, close your files.

> well-written Go does not panic

But sometimes it does. And if your code is not exception safe then you turn a safe problem into, essentially, undefined behaviour.

> and if it does, execution will end very soon.

Nope, not if panicing in an HTTP handler. If these two HUGE examples I've given you didn't exist then I would likely agree with you.

But I'm going to declare knorker's law: Every piece of code ever written will at some point be called from an HTTP handler.

> A panic in a String() method is just bad code, don't do it.

That's too much "just don't write bugs". Modern languages are supposed to be safe, no?

> in C++ you can reasonable expect an exception anywhere

Yes, just like in Go you never know in what context your code will run in the future. Which means that in both Go and C++ you need to write exception-safe code even if you never throw or catch exceptions.

> [C++ exception for] IO failures […in idiomatic C++]

Oh? The standard library doesn't do this, right? What makes you say that's idiomatic C++?

Idiomatic Java, sure, but Java is just crazy about exceptions for everything.

> [C++ exceptions for] invalid states [… in idiomatic C++]

Idiomatic Go also calls for panic for the "can't happen" case.

> other errors that should be expected in the execution of a program all throw exceptions in idiomatic C++.

What makes you say this is idiomatic C++?

I've seen Go code that does it too, but that doesn't make it idiomatic Go.

Does the C++ standard library do this? The closest thing I can think of is std::bad_alloc on OOM, but then again Go panics then too.

Re: Is C++ Doomed?

#153
post #45

I am very confused. Some of these ramblings are just plain wrong, like > But exceptions come at a performance cost. No they don't. An exception that isn't thrown costs nothing. Also > Let’s say I want to know if a constructor failed. I have two options, one is to pass in an in-out parameter, the other is to the throw an exception. Using a factory function instead is a third option that avoids this. Besides, this is a…

> No they don't. An exception that isn't thrown costs nothing. Wait whaaat? When every expression can throw anything, the compiler and the user both loose the ability to reason about code locally. They change any function from returning one value of one type, to returning any value of any type. This: - has a performance cost: it breaks lots of compiler optimizations that require knowing where and what can functions r…

it's obvious that OP meant a performance cost - as generally when you program in C++ that's the n°1 metric. And for that one, exceptions as they are today are often faster than the equivalent error-code-based code: https://nibblestew.blogspot.com/2017/01/measuring-execution-...

Of course if we can get even faster exceptions such as the throwing values model, by all means let's do it... i'm entirely fine with recompiling literally every software / library I use if that can get me half a % more performance

Re: Is C++ Doomed?

#154
post #94
post #66

Earlier quoted context omitted.

> The author seems to look for something like Go instead But in Go you also have to write exception safe code, something the author complained about. Before someone says "but you're not supposed to throw or catch in Go": the std http server swallows exceptions thrown in handlers, and fmt.Print does for String callbacks too. And before someone says Go doesn't have exceptions: It does, 100%, in all but name. I actually…

> And before someone says Go doesn't have exceptions: It does, 100%, in all but name. But here the name actually matters. They're called "panic"s, not exceptions. When you panic, you signal a fatal error that should ideally terminate the program. You shouldn't care about closing files or freeing memory, because well-written Go does not panic, and if it does, execution will end very soon. A panic in a String() method…

> But here the name actually matters.

no, what matters is the language semantics. It could call itself "bamboozle" or whatever, the only thing that is relevant is what the compiler and CPU will do when reaching that code.

> idiomatic C++.

idiomatic is I think a big anti-pattern, as it creates assumptions and religious wars where there should not be. You've got a tool, you can use it in any way that fits the situation you're in. Is using a hammer to put a screw in a wall an idiomatic way to use a hammer/screw ? most certainly not. That does not mean that when you just have a hammer and a screw available you should throw your hands up in the air and complain that the gods put you in un-idiomatic situations.

Re: Is C++ Doomed?

#155
post #148

Earlier quoted context omitted.

Exceptions are the only way. You can workaround this by using static factory methods. The herbceptions proposal for “deterministic” exceptions addressed this issue well IMO.

I doubt that proposal ever gets adopted.

I also doubt it

Re: Is C++ Doomed?

#157
post #118

Earlier quoted context omitted.

I don't understand the obsession with constructors. Placement new exists, but I'm not sure that's what you asked w.r.t. placement initialisation, which doesn't make much sense for non-trivial types anyway. Fallible placement initialisation into allocated memory is something that I honestly don't know a good use case for. It's certainly not something that's very common and I never encountered it. It's just one of thos…

> I don't understand the obsession with constructors. The article describes how smart pointers are more difficult to use for opaque/POD structs without destructors. Correct me if I'm wrong, but I think types with destructors generally require constructors too (though you can sometimes rely on default destructors/constructors, by holding fields which themselves have destructors/constructors). > Fallible placement init…

> I think types with destructors generally require constructors too

If by that you mean explicit constructors, then no. The compiler will happily generate an empty default ctor for you. Ctors in general are only useful to establish a known initial state and uphold class invariants.

If neither of these applies (e.g. no class invariant or delayed initialisation), no ctor is necessary (or even wanted for that matter - initialisation is not free).

> Copying can be a correctness issue as well as performance.

Agreed, but that is just a matter of language semantics. Take the opposite for example: in Java every non-primitive is passed by reference. You get the opposite problem and now have to deal with (sometimes implicitly) boxed values and cumbersome cloning-interfaces. C++ has references and (const-) pointers and it's the programmer who decides when to use which option.

The language itself doesn't limit or restrict the programmer in that regard and unlike Java it's more consistent in its semantics (i.e. no ominous "value types" that require boxing and are not passed by reference).

Re: Is C++ Doomed?

#159
post #140
post #17

The basic problem with C++ is that it has has hiding without safety. C has neither, and most newer languages have both. Attempts to add safety to C++ via templates always seem to leak raw pointers, since many APIs never converted to C++. It's significant that there isn't a C++ Linux kernel API, where you use C++ strings for everything and get rid of the null-terminated stuff. You see this in the original poster's exa…

C++ is a multi paradigm language, it is not “object oriented” necessarily. also, there is nothing wrong with C++. it’s C with additional language constructs and the STL. use as much or as little as you want of it. also, there is no such thing as leaking a pointer. you can leak a piece of memory. at the end of the day you need to understand operating systems and how memory management, CPUs and instruction sets work an…

What other paradigms are popular with C++? Never came across anything else other than object programming.

Re: Is C++ Doomed?

#160

Earlier quoted context omitted.

In this aerospace company, we use C, no C++ allowed, to avoid all the extra footguns that get in the way of safety and certification. Rust will probably be the next choice on the scene, but I expect it'll take as long to come as Ada took to go.

Off topic, but curious why Ada went?

Ada relied on colleges teaching it or Pascal, so then it became hard to hire. I understand the rationale behind this, but the language is surprisingly easy to learn because it's usually explicit and straightforward.

It suffered from two decades of bad press due to being forced by the DoD and then almost two decades of no press. Looking at Google ngrams, C (due to Unix) took big chunks out of it and then Java gave the death blow by becoming a major teaching language. Ada 2012 had the capability to do for Ada what C++11 did for C++, but it wasn't sold well.

I picked up Ada 2012 last year and made a tool I use everyday now for real work--it's not a bad language. It suffers from a lot of myths about it, but the tool modernization like getting a package manager makes it productive. It is "boring" in the sense that there's nothing flashy about it, and its surface verboseness which saves other code later, and lack of curly braces turns people off. The community is super small and nice, but it never learned to sell the benefits of the language.

Post reply on HN