Observations about the C++ Standard library

  Рет қаралды 2,252

CopperSpice

CopperSpice

Күн бұрын

Пікірлер: 13
@tourdesource
@tourdesource Жыл бұрын
Reading STL code is like staring into the Matrix ─ only you guys could possibly make the experience enjoyable. Thank you!
@LukaszGwizdz
@LukaszGwizdz 2 жыл бұрын
Please correct me if I'm wrong, but... As far as I know, std::launder() is necessary if a new object is created at the storage location which the original object occupied ONLY if the type of the original object was const-qualified or contained non-static data members whose types were const-qualified or they were reference types. Otherwise, a pointer (without std::launder(ptr)) to the original object can be used to refer to the new object. And it was introduced in C++17, not C++20.
@CopperSpice
@CopperSpice 2 жыл бұрын
In our example where the object being created is a Widget you would need to know the full implementation of this class to bypass std::launder and not have UB. Every data member of Widget must be non-const, not a reference, and a few other requirements. This may not be something you would know or could be guaranteed to remain true. The standard library code we showed is a template and you can not make any assumptions about what the T contains. It is simply too dangerous to make guesses like this.
@neoprout
@neoprout 2 жыл бұрын
As ai understand std::launder, its uses are to tell the compiler that an object name, reference, or non static data member could refer to a stale object, in which case std::launder needs to be applied on this name to force the compiler to drop any cached value and do a 'real' access. For modifiable objects or subobjects, the compiler always assumes an object name or reference may have become stale, so std::launder is not necessary when accessing the object through that name. For const qualified names (of trivial types, I think) and reference data members, the compiler always assume that their value never change (at least, using conventional ways like a builtin assignment), but it could still done by reusing the storage, or by starting the lifetime of a new object in the same storage (by use of a placement new or changing the active member of a union); this is in this very precise case that a launder on the object name or reference is necessary. Note that, since dynamic allocation of references is impossible, and that dynamic allocation of const qualified memory usually never occurs (partly because the Allocator requirement forbids any cv qualifiers, hence there is no container with a const value_type), I would expect that STL containers or allocator implementation would never need to call std::launder on a freshly placement-constructed object of type value_type (although this could occur sometimes on the key_type of some assoc container).
@neoprout
@neoprout 2 жыл бұрын
But casting from a void pointer, I pretty much think the compiler is required to do a full fetch on the Widget object, even if the pointer itself would be const qualified (making std::launder redundant). If we think about this, since a void pointer cannot be dereferenced and read directly, how could one keep a cached value of a yet to come static cast to a not yet know type. The compiler can't predict if the placed Widget would be in its lifetime at the very next static cast from the void pointer, or if it would be ended and another object type is placed there by the time we do a static cast from the void pointer; so the compiler cannot establish type based lifetime related relationships between the placement new expression that creates the Widget and very next expression that will access this object through that void pointer, hence it cannot cache the result of the placement new and return this cached value as the result of the static cast. Instead, it must treat the result of the static cast as an unrelated Widget from the one created in line A. Hence making launder redundant.
@cpp1158
@cpp1158 2 жыл бұрын
Great work , please continue 🤗
@Bob-zg2zf
@Bob-zg2zf 2 жыл бұрын
two months have passed. next video pleaaaaaaaaaz!
@CopperSpice
@CopperSpice 2 жыл бұрын
Funny you should ask, we have a new video coming out next week!
@Bob-zg2zf
@Bob-zg2zf 2 жыл бұрын
@@CopperSpice 😍
@notebook2866
@notebook2866 2 жыл бұрын
ok but is LGPL forbidden to be linked statically?
@CopperSpice
@CopperSpice 2 жыл бұрын
The license of your application is not tied to the C++ Standard Library. Your application can be linked statically with a third party library which has an LGPL license provided you release the source code for any changes in the library and the object files for your application.
@jackh4158
@jackh4158 2 жыл бұрын
Thanks! Very interesting :)
@IllumTheMessage
@IllumTheMessage 2 жыл бұрын
Well done.
Is Virtual of Value
13:37
CopperSpice
Рет қаралды 1,5 М.
Auto is Not a Data Type
16:54
CopperSpice
Рет қаралды 1,9 М.
To Brawl AND BEYOND!
00:51
Brawl Stars
Рет қаралды 17 МЛН
How Strong Is Tape?
00:24
Stokes Twins
Рет қаралды 96 МЛН
char8_t
16:16
CopperSpice
Рет қаралды 2 М.
Can C++ be 10x Simpler & Safer?  - Herb Sutter - CppCon 2022
1:54:00
What is an object pool, and how to create one in C?
23:14
Jacob Sorber
Рет қаралды 14 М.
Python laid waste to my C++!
17:18
Sheafification of G
Рет қаралды 190 М.
Should I pass by const reference or by value?
10:45
The Cherno
Рет қаралды 113 М.
Stack vs Heap Memory in C++
19:31
The Cherno
Рет қаралды 587 М.
Every Programming Language Ever Explained in 15 Minutes
15:29
Flash Bytes
Рет қаралды 366 М.
Writing My Own Database From Scratch
42:00
Tony Saro
Рет қаралды 265 М.
Paradigms, Polymorphism, and Generic Programming
16:40
CopperSpice
Рет қаралды 1,7 М.
How C++ took a turn for the worse
5:03
Code Persist
Рет қаралды 334 М.