Copy Elision

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

@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.

MinhTran-wnri
Автор

These c++ videos are EXCELLENT! Thank you very much for posting!

danwest
Автор

🎵 Copy Elision down the road that I must travel. Copy Elision through the darkness of the night. 🎵 🤣

kbsanders
Автор

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.

zsoltory
Автор

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

Betheev
Автор

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.

kormisha
Автор

Good to know, thanks.
Do we expect to have garbage collectors like Java and C# for C++ in the future?

tomay
Автор

hmm... slide at about 3m30s suggests data_d triggers operator=()?? shouldnt it be copy ctor?

gusromul
visit shbcf.ru