[讀書筆記]Universal References in C++11 part 2: Move semantics, lvalue reference and rvalue reference
上一篇: [insert page=’309′ display=’link’] C++11以前,效能上最大的瓶頸就是程式執行過程中,會產生許多不必要的臨時物件,並進行許多昂貴的複制操作。從上一篇的文章中我們知道,C++11導入『T&&』語意來解決這個問題。或許有人會問,這個問題不是早就有相關機制能夠解決了嗎?將function的傳入參數指定為reference或pointer不是就能避掉無謂複制操作?例如下面實作的三種myCopyFunction,這個函式的主要用途是將傳入的參數copy給global 物件myBackupWidget: class Resource{ public: std::string m_data; }; class Widget{ public: Widget() : m_pHeapStorageResource(nullptr){ } public: Resource* m_pHeapStorageResource; Resource m_stackStorageResource; int m_i; }; static Widget g_myBackupWidget; void myCopyFunc1(Widget param){ g_myBackupWidget.m_pHeapStorageResource = param.m_pHeapStorageResource; g_myBackupWidget.m_stackStorageResource = param.m_stackStorageResource; g_myBackupWidget.m_i … Continued