Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to overload methods accepting const lvalue reference for rvalue references explicitly?

currently I’m playing around with rvalue reference (C++11, g++ with gnu++x0) and I want to implement move semantics in my classes, because it just feels „right“.

Do I need to overload each function which normally would accept const lvalue reference to benefit from the rvalue references?

Let’s say this is my example class:

class Person {
public:
    Person() = default;
    Person(std::string &name);
    Person(const Person &rhs);
    Person(Person &&rhs);

    Person& operator=(const Person &rhs);
    Person& operator=(Person &&rhs);

    std::string& get_name() const;
    void set_name(const std::string &name);
private:
    std::string name_;
}

/* snip */

void Person::set_name(const std::string &name)
{
    this->name_ = name;
}

I now want to use the setter with rvalue references and eliminate unnecessary copies.

Person test("Jon Doe");
test.set_name("Jon Doe2");

Do I really need to overload every method this way?

void Person::set_name(std::string &&name)
{
    this->name_ = std::move(name);
}

This seems very redundant to me. Is there any way to implement this easier?

Thanks!

(I read stackoverflow often, but this is my first question. So please give me hint if I’m doing something wrong.)

like image 235
Stefan Haller Avatar asked Sep 28 '11 17:09

Stefan Haller


People also ask

Can you pass an lvalue to an rvalue reference?

In the example, the main function passes an rvalue to f . The body of f treats its named parameter as an lvalue. The call from f to g binds the parameter to an lvalue reference (the first overloaded version of g ). You can cast an lvalue to an rvalue reference.

Can rvalue bind to lvalue?

In this example, the rvalue reference a can be bound to the temporary initialized with the rvalue expression 2 , but the rvalue reference b cannot be bound to the lvalue expression i . You can bind the rvalue reference c to the temporary value 1.0 that is converted from the variable i . End of C++11 only.

Is const reference an lvalue?

Such a reference is called an lvalue reference to a const value (sometimes called a reference to const or a const reference). In the above program, we bind const reference ref to modifiable lvalue x . We can then use ref to access x , but because ref is const, we can not modify the value of x through ref .

How do rvalue references work?

An rvalue reference is formed by placing an && after some type. An rvalue reference behaves just like an lvalue reference except that it can bind to a temporary (an rvalue), whereas you can not bind a (non const) lvalue reference to an rvalue.


1 Answers

Write one function. Take it in by value, then move it.

void Person::set_name(std::string name)
{
    this->name_ = std::move(name);
}

Let the std::string decide how to copy itself.

like image 199
Benjamin Lindley Avatar answered Oct 17 '22 00:10

Benjamin Lindley