Skip to content

Conversation

@cjbacchus
Copy link

No description provided.

@PetrilloAtWork
Copy link
Owner

My idea was that this would be a suggestion for people lazily wondering "what do I do?". The motivation of the recommendation is harder to understand than the constant reference one, but it's more efficient too.
Although there is still debate and there are notable protesters (sorry, I couldn't find a certain page supporting an opposite view).
I would like other opinions on this. I do commit to make a more detailed explanation if we keep it (that would land in the famous "part II" with details for the inquisitive reader).


For example, why not pass initialHits as a reference above and then not move it?

In my understanding, in that situation passing a temporary value copies it at assignment or construction time:

struct Data {
  std::vector<int> values;
  Data(std::vector<int> const& v): values{ v } {}
};

std::vector<int> initData;
Data data1 { initData };
Data data2 { std::vector<int>{} };

Here data1 gets a copy of initData, and data2 gets a copy of std::vector<int>{} (both via values{ v }).

Instead:

struct Data {
  std::vector<int> values;
  Data(std::vector<int> v): values{ std::move(v) } {}
};

std::vector<int> initData;
Data data1 { initData };
Data data2 { std::vector<int>{} };

Here data1 gets a copy (via std::vector<int> v) + move of initData (via values{ std::move(v) }), and data2 gets a move of std::vector<int>{} (via values{ std::move(v) }).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants