4 ms·
Everything in C++ is just there to solve a weird interaction between <feature that’s in your codebase> and <feature that’s also in your codebase>. The “just lea
by codeflo 5y ago
Everything in C++ is just there to solve a weird interaction between <feature that’s in your codebase> and <feature that’s also in your codebase>. The “just learn a subset” crowd is wrong, you really do need to know everything.
- ATsch 5y agoYou also need to learn what placement new is and how it is used to know if you or any of your libraries are using it in the first place.
- Rexxar 5y agostd::vector use placement new and you don't need to know it to use std::vector correctly.
- ATsch 5y agoIf you don't understand what placement new is, you don't know whether libraries using placement new affects you. I did not write this comment about placement new specifically. It's just a thing that annoys me about the "learn a subset" idea. You still need to know at least enough about every feature to know whether it affects you. Placement new is perhaps not the best example there because it is relatively niche and self contained, true.
- google234123 5y agoIf you don't understand all the 10s of million of line of linux code, you don't know whether you are using linux correctly.
- spoiler 5y agoIf you're going to try to be clever/snide, at least come up with a better analogy... Using Linux effectively doesn't mean you need to learn its source code. You need to know about its features (and trade-off, etc). You don't need to know a language implementation's (e.g. gcc/clang for C++) source code to use a language well either, do you? Obviously you don't; it's a fools errand if all you ever want is to use it effectively. You need to know about features (and trade-off, etc), how they were designed in isolation, and more importantly how they were designed to fit/connect together.
- google234123 5y agoShow me some basic code using std vector that would change if the programmer knew more about placement new.
- google234123 5y agoNo you don’t.
- saagarjha 5y agoNot if they don’t expose it to you via their API. Very few libraries will do this.
- gpderetta 5y agoInterestingly one of the reasons launder exist is that without it std::vector is not implementable within the strict letter of the standard. IIRC the issue was that vector construct one object at a time but then access it as if it was part of an array which was never constructed
- aaaaaaaaaaab 5y agoGotta love these C++ FUD arguments! Can you tell us an example where a library’s internal usage of placement new results in bugs unless the consumers use std::launder?
- tsimionescu 5y agoIsn't this exactly what this argument shows? In older versions of C++, if you have an object with a non-const sub-object with a const member, and you give this object to a library, and then that library uses placement new to replace the sub-object within your object, you had to use std::launder to keep referring to the fields of your object, or you'd invoke UB. In newer C++, thankfully, this is no longer required. Edit: found a committee paper discussing exactly such cases: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0532r0.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p053... Obj x; x.use(); // ok mutate(x); // does this call placement new? x.use(); // may have undefined behavior std::launder(&x)->use(); // may be necessary x.use(); // may still have undefined behavior