Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rvalue for a std::string parameter

What's the difference in practice between LVALUE and RVALUE in the following code when I pass the text? I mean, in this specific case of a string (where the string is a string literal), is there any benefit of using RVALUE (&&)?

void write_Lvalue(const std::string &text) {
    //...
}

void write_Rvalue(const std::string &&text) {
    //...
}

int main() {
    write_Lvalue("writing the Lvalue");
    write_Rvalue("writing the Rvalue");
}
like image 554
anc Avatar asked Apr 10 '19 18:04

anc


People also ask

What is R value Referene in C ++ 11?

In C++11, however, the rvalue reference lets us bind a mutable reference to an rvalue, but not an lvalue. In other words, rvalue references are perfect for detecting whether a value is a temporary object or not.

How do you pass rvalue reference to a function?

If you want pass parameter as rvalue reference,use std::move() or just pass rvalue to your function.

What is R value reference in C++?

Rvalue references is a small technical extension to the C++ language. Rvalue references allow programmers to avoid logically unnecessary copying and to provide perfect forwarding functions. They are primarily meant to aid in the design of higer performance and more robust libraries.

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.


1 Answers

First, constant rvalue reference are not really useful, since you cannot move them. Moving value need mutable references to work.

Let's take your corrected example:

void write_lvalue(std::string const& text) {
    //...
}

void write_rvalue(std::string&& text) {
    //...
}

int main() {
    write_lvalue("writing the Lvalue");
    write_rvalue("writing the Rvalue");
}

In this case, the two are completely equivalent. In these two case, the compiler has to create a string and send it by reference:

int main() {
    // equivalent, string created
    // and sent by reference (const& bind to temporaries)
    write_lvalue(std::string{"writing the Lvalue"}); 

    // equivalent, string created
    // and sent by reference (&& bind to temporaries)
    write_rvalue(std::string{"writing the Rvalue"});
}

So why have function that takes rvalue references?

It depends on what you do with the string. A mutable reference can be moved from:

std::string global_string;

void write_lvalue(std::string const& text) {
    // copy, might cause allocation
    global_string = text;
}

void write_rvalue(std::string&& text) {
    // move, no allocation, yay!
    global_string = std::move(text);
}

So why using rvalue reference at all? Why not using mutable lvalue reference?

That is because mutable lvalue references cannot be bound to temporaries:

void write_lvalue_mut(std::string& text) {
    // move, no allocation... yay?
    global_string = std::move(text);
}

int main() {
    std::string s = /* ... */;
    write_lvalue_mut(std::move(s)); // fails
    write_lvalue_mut("some text"); // also fails
}

But mutable rvalue reference can be bound to rvalue, as shown above.

like image 64
Guillaume Racicot Avatar answered Sep 18 '22 06:09

Guillaume Racicot