Project

General

Profile

Feature #1464

Enhance performance by utilizing std::move for rvalues to the max

Added by Oliver Maurhart 5 months ago. Updated 5 months ago.

Status:
New
Priority:
Low
Assignee:
-
Target version:
Start date:
31.07.2018
Due date:
% Done:

0%

Estimated time:

Description

Avoid unnecessary copies of data (esp. in constructors) by utilizing the std::move operator excessively.

class TextBox
{
public:
   explicit TextBox(std::string text) : text_(std::move(text)) {}
private:
   std::string text_;
};

What’s going on here? If we pass it an lvalue, the copy constructor of std::string gets called to construct the text parameter (one copy), then text is moved into text_ (no copy).
And if we pass it an rvalue, the move constructor of std::string gets called to construct the text parameter (no copy), and then text is moved into text_ (no copy).

see: https://www.fluentcpp.com/2018/07/17/how-to-construct-c-objects-without-making-copies/

History

#1 Updated by Oliver Maurhart 5 months ago

  • Description updated (diff)

Also available in: Atom PDF