Copy Elision

  Рет қаралды 4,349

CopperSpice

CopperSpice

Күн бұрын

Пікірлер: 22
@MinhTran-wn1ri
@MinhTran-wn1ri 4 жыл бұрын
@4:57 *data_c* is default-constructed (parameterless constructor invoked) then Widget's *copy constructor* (not copy assignment) is called to _construct_ data_d. The copy assignment operator is invoked when you're assigning one initialized Widget to another, already initialized, Widget: Widget data_c; Widget data_d; data_c = data_d; // Copy assignment invoked to copy data_d Widget data_e; Widget data_f = data_e; // Same as Widget data_f{data_e}; which invokes copy constructor.
@bwatspro
@bwatspro 4 жыл бұрын
Best Cpp tutorials on youtube
@zsoltory4176
@zsoltory4176 4 жыл бұрын
Unnecessary copies, where heap storage is involved, like e. g. copying std::sets or std::maps, where new possibly small chunks of memory have to be allocated for the copy, where the original chunk copied from still remains on the heap, may lead to avoidable heap fragmentation. Heap fragmentation bloats the heap with unusable small chunk and thus decreasing the effectively usable heap. Thus moving data wherever possible may also prevent heap fragmentation. In addition, storage allocation on a fragmented heap is not only more effective but also faster.
@CopperSpice
@CopperSpice 4 жыл бұрын
Did you have a question about Copy Elision?
@zsoltory4176
@zsoltory4176 4 жыл бұрын
@@CopperSpice No, I just made the remark above. The effect described there considerably improved heap consumption and speed of one of my programs.
@Betheev
@Betheev 4 жыл бұрын
AFAIK, copy assignment only takes place when the variable being assigned a value to had already been created in a previous step. If it is created in the same step, the copy constructor is called. I quicky tested the second example with std=C++11 -fno-elide-constructor and the assignment operator is not called
@CopperSpice
@CopperSpice 4 жыл бұрын
You mentioned "previous step" and "same step" which is a bit vague since there are no "steps" in C++ code. I am not sure if this refers to a statement, an expression, or a subexpression, which are all a bit different. One of the issues with testing compiler options is that optimization is difficult to observe. Running an experiment tells you how your compiler interprets the code, not how the standard defines it. The purpose of these examples is to illustrate that the compiler was allowed to elide prior to C++17, but this elision is guaranteed by the C++17 standard.
@danwest9900
@danwest9900 4 жыл бұрын
These c++ videos are EXCELLENT! Thank you very much for posting!
@CopperSpice
@CopperSpice 4 жыл бұрын
Thanks so much for your comment. It's great to know when people find our videos useful. Please let everyone know about our channel!
@danwest9900
@danwest9900 4 жыл бұрын
@@CopperSpice I will!
@kormisha
@kormisha 4 жыл бұрын
One if the first rules regarding this I learned that construction of a new object using the assignment is always by a copy constitution; the assignment operator is not involved. I do not think that is a compiler dependent.
@kbsanders
@kbsanders 4 жыл бұрын
🎵 Copy Elision down the road that I must travel. Copy Elision through the darkness of the night. 🎵 🤣
@stephanbokelmann507
@stephanbokelmann507 4 жыл бұрын
Awesome explanation! Thanks
@josephlagrange9531
@josephlagrange9531 2 жыл бұрын
I love your work!
@A1Googler
@A1Googler 4 жыл бұрын
Thank you for posting.
@gusromul3356
@gusromul3356 Жыл бұрын
hmm... slide at about 3m30s suggests data_d triggers operator=()?? shouldnt it be copy ctor?
@gusromul3356
@gusromul3356 Жыл бұрын
oh, sorry, i should have read the other posts before posting the above (i see ppl have already pointed out the issue).. anyhow, great videos nevertheless
@tomay3000
@tomay3000 4 жыл бұрын
Good to know, thanks. Do we expect to have garbage collectors like Java and C# for C++ in the future?
@CopperSpice
@CopperSpice 4 жыл бұрын
It turns out garbage collection is an optional feature in the standard. The problem is that GC is really complicated and has a performance impact. If you call std::get_pointer_safety() and the return value is "strict" then your C++ standard library *may* provide a garbage collector. From what we know, no current standard library provides it.
@RoyalGun-9mm
@RoyalGun-9mm 2 жыл бұрын
great work
@hasanrasulov3502
@hasanrasulov3502 3 жыл бұрын
Thank you
@Bob-zg2zf
@Bob-zg2zf 4 жыл бұрын
Lovely!
Qualifiers
12:31
CopperSpice
Рет қаралды 2,6 М.
Mutex + Lock = CsLibGuarded
14:00
CopperSpice
Рет қаралды 2,2 М.
We Attempted The Impossible 😱
00:54
Topper Guild
Рет қаралды 56 МЛН
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
36:55
bayGUYS
Рет қаралды 1,9 МЛН
Сестра обхитрила!
00:17
Victoria Portfolio
Рет қаралды 958 М.
Return Value Optimization and Copy Elision in C++
9:54
mCoding
Рет қаралды 40 М.
Modern C++ (move semantics, perfect forwarding)
19:49
CopperSpice
Рет қаралды 22 М.
Type Traits
13:37
CopperSpice
Рет қаралды 14 М.
Move Semantics in C++
13:10
The Cherno
Рет қаралды 308 М.
Is Virtual of Value
13:37
CopperSpice
Рет қаралды 1,5 М.
Constexpr Static Const
19:52
CopperSpice
Рет қаралды 7 М.
CppCon 2018: Jon Kalb “Copy Elision”
5:02
CppCon
Рет қаралды 18 М.
Auto is Not a Data Type
16:54
CopperSpice
Рет қаралды 1,9 М.