News for hackers. Stuff that matters.
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.