isocpp / isocpp/CppCoreGuidelines

F15, in & retain copy, why not X?

Open
#759 2 comments 0 reactions 1 assignee View on GitHub

@hsutter is already working on this.

Since Apr 3, 2017.

Dominant language
CSS
Stars
45.3k
Forks
5.6k
PR merge metrics
No merged PRs in 30d

Description

Hi,

I'm not sure if this is the right forum to ask, and sorry if this has been asked before..
For parameter passing, when called function want to retain a copy, and the parameter is cheap to move, wouldn't be "X" is the simplest solution, and then inside the function move out from that? That gets rid of the need for two functions or templated version, and still leaves the opportunity to retain a copy for the caller too.

For example:

class Foo
{
    std::vector<int> mThings;
public:
    void SetThings(std::vector<int> Things)
    {
        mThings=std::move(Things);
    }
};

And this way the caller still has option to retain copy:

Foo MyFoo;
MyFoo.SetThings(std::move(MyThings));

or if it does not want to retain a copy, it can use move:

MyFoo.SetThings(MyThings);

As I understand the code on the caller side would be the same with the current recommendation; but I think the called side is simpler.

What is it that I am missing?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.