News for hackers. Stuff that matters.
Re:I Have No Constructor, and I Must Initialize by GuB-42 on Friday July 05, 2024 @10:13AM (#40882918)
C++ is a popular multi-paradigm language that is both cutting edge and 40 years old (more if you count C), there is simply no way around that level of complexity.

You have "C with classes" that coexist with the "modern" way, full of smart pointers and functional programming. It is popular in embedded systems, video games, servers, and GUIs (mostly Qt). And if you look at the code, it is as if it was a different language, because the requirements are all very different. Embedded system need low level hardware access, video games are all about performance, servers want safety, and GUIs want flexibility.

There are less awful alternative to C++. For example C on one end of the spectrum and Rust on the other end. But none of them cover every C++ use case.

Re:I Have No Constructor, and I Must Initialize by marcosdumay on Friday July 05, 2024 @12:08PM (#40883928)
> multi-paradigm

Well, it does unstructured imperative, structured imperative, and OOP imperative!

Except if you count template programming, because that one is pure functional, but only runs at compile time.

Re:I Have No Constructor, and I Must Initialize by hu3 on Friday July 05, 2024 @12:01PM (#40883878)
Zig looks promising too.
Re:I Have No Constructor, and I Must Initialize by bee_rider on Friday July 05, 2024 @12:38PM (#40884155)
C++ needs a different name from multi-paradigm. Java is a multi-paradigm language. C++ is an omni-paradigm language. If there’s a paradigm,

- There’s at least an ugly library to do it in C++

- There might be support baked directly into the language

- Or you could do it in Lisp, but that would be too easy

Re:I Have No Constructor, and I Must Initialize by codeflo on Friday July 05, 2024 @02:03PM (#40884768)
And if you dare to combine two of the paradigms it supports, you get UB.
Re:I Have No Constructor, and I Must Initialize by snappythrowaway on Friday July 05, 2024 @03:42PM (#40885415)
What is UB?
Re:I Have No Constructor, and I Must Initialize by MaxBarraclough on Friday July 05, 2024 @04:33PM (#40885756)
Undefined behaviour.

Here's an article on the topic: https://cryptoservices.github.io/fde/2018/11/30/undefined-be...

Re:I Have No Constructor, and I Must Initialize by shadowgovt on Friday July 05, 2024 @08:09PM (#40887185)
It's what happens when you make a union of an int and a float and write it as an int and read it as a float.

Most compilers will do something like "treat the bits like they represent a float, even though they mean something else when they're treated as an int."

But the language spec says the compiler is allowed to send an email to Bjarne Stroustrup with your home address so he can come over and personally set your computer on fire.

Re:I Have No Constructor, and I Must Initialize by GuB-42 on Sunday July 07, 2024 @01:52PM (#40899198)
I bet on Bjarne Stroustrup being too busy setting other programmers computers on fire before coming to mine.

More seriously, the typical response to undefined behavior is for the compiler to optimize out whatever code may trigger it. It is great for performance and one of the reasons C and C++ often top the charts, but it may lead to weird bugs that depend on the optimization level.

For the union case, the compiler could completely remove the code that reads the float: because it is UB, it is something you shouldn't do, so it considers that you will never do it, so it can just remove the code. In the end your conversion function may do nothing at all (very optimized!). In practice, because it is so common, even though it is UB, it will usually do what you want it to do.

Re:I Have No Constructor, and I Must Initialize by aaroninsf on Friday July 05, 2024 @01:35PM (#40884551)
> But none of them cover every C++ use case.

Literal lol... this is not an argument in favor of C++.

Re:I Have No Constructor, and I Must Initialize by slashdave on Friday July 05, 2024 @02:01PM (#40884745)
I would sort of agree, except when c++ was invented, it was even more awful in practice (does anyone remember the chaos around STL and template caches?). So, age isn't really a factor.
Re:I Have No Constructor, and I Must Initialize by wredue on Friday July 05, 2024 @03:22PM (#40885284)
If you’re looking for a language with less complexity than C++, you’re surely not going to find that in rust.
Re:I Have No Constructor, and I Must Initialize by kelnos on Friday July 05, 2024 @05:56PM (#40886396)
I disagree. To me, the complexity described in this article is more complex than anything you'll find in Rust.

Actually, strike that: I'm not sure if it's true or not (though I suspect it is), but it doesn't actually matter. What I'm really getting at here is that there is nothing in Rust that behaves so confusingly or ambiguously as what's described in this article. If you're writing Rust, you'll never have to remember these sorts of rules and how they are going to be applied to your code.

I do agree that reading someone else's Rust can be a challenge, if they're using Rust's type system to its fullest, and you're (quite reasonably and understandably) not up to speed on the entirety of it. And that is a problem, agreed; though, at least, fortunately it's not a problem of ambiguity, but more of a learning-curve issue.

But I have never been writing Rust and have been confused about what the code I'm writing might do, never had to consult and puzzle out some obscure documentation in order to ensure that the code I was writing was going to do what I expected it to do. C++ falls so incredibly flat in this department, and that's why I avoid using it like the plague.