▲ | monkeyelite 5 days ago | ||||||||||||||||||||||||||||||||||||||||||||||||||||
The complexity argument is just not true. You do have to know this stuff in c++, you run into it all the time. I wish I didn’t have to know about std::launder but I do | |||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | jandrewrogers 5 days ago | parent | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
You need something like std::launder in any systems language for certain situations, it isn’t a C++ artifact. Before C++ added it we relied on undefined behavior that the compilers agreed to interpret in the necessary way if and only if you made the right incantations. I’ve seen bugs in the wild because developers got the incantations wrong. std::launder makes it explicit. For the broader audience because I see a lot of code that gets this wrong, std::launder does not generate code. It is a compiler barrier that blocks constant folding optimizations of specific in-memory constants at the point of invocation. It tells the compiler that the constant it believes lives at a memory address has been modified by an external process. In a C++ context, these are typically restricted to variables labeled ‘const’. This mostly only occurs in a way that confuses the compiler if you are doing direct I/O into the process address space. Unless you are a low-level systems developer it is unlikely to affect you. | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | ryao 5 days ago | parent | prev [-] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
I feel like C++ is a bunch of long chains of solutions creating problems that require new solutions, that start from claiming that it can do things better than C. Problem 1: You might fail to initialize an object in memory correctly. Solution 1: Constructors. Problem 2: Now you cannot preallocate memory as in SLAB allocation since the constructor does an allocator call. Solution 2: Placement new Problem 3: Now the type system has led the compiler to assume your preallocated memory cannot change since you declared it const. Solution 3: std::launder() If it is not clear what I mean about placement new and const needing std::lauder(), see this: https://miyuki.github.io/2016/10/21/std-launder.html C has a very simple solution that avoids this chain. Use structured programming to initialize your objects correctly. You are not going to escape the need to do this with C++, but you are guaranteed to have to consider a great many things in C++ that would not have needed consideration in C since C avoided the slippery slope of syntactic sugar that C++ took. | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|