0

How to move string | const ref

Hi Refer code below: I am concerned about set method. As it needs a copy constructor of std::string , I want to avoid that expensive operation . One thought came to my mind is use rvalue as below: void set(string&& s) {m_str = move(s);} This avoid copy constructor , but it will not work for second case in main function (that is l value). So, I thought as below: void set(const string& s) {m_str = move(s);} This works for both cases of lvalue and rvalue. Is this valid code ? Confusion is about move and const both. move modify the s and set it to empty but still we are string that const string& s providing confirmation that s is not updated by function. https://sololearn.com/compiler-playground/cosEyAgQqz5P/?ref=app

26th Sep 2024, 8:16 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
6 Respuestas
+ 2
const ref&& is for rvalue and will not work here or is a bad idea to do. It is better you use the old implementation for this problem. You will set pointer instead mystr a; set(&a);
26th Sep 2024, 3:38 PM
Sharpneli
Sharpneli - avatar
0
there's a trick in the second version of <my_string::set> you need to be aware of it, namely that " m_str = std::move(s); " doesn't actually move anything. It merely tells the compiler to try to move. however, since <std::string> class has no "assignment operator" that accepts a "const std::string&&", it will instead use the "const std::string&" copy assignment operator, and safely copy assign. no danger, no trap. see yourself: https://sololearn.com/compiler-playground/cD5MZA3uiu1G/?ref=app
26th Sep 2024, 3:56 PM
MO ELomari
MO ELomari - avatar
0
Thanks MO ELomari, but I could not digest this information. Let me go one by one to have my understanding better. 1. Is it not guaranteed that object is moved always when we have used std::move? 2. What are these const rvalue ref functions? I thought we don't have any function which has const T&& as const and r value ref cannot be together.
27th Sep 2024, 11:04 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
1: <std::move> call doesn’t move anything. It is just a request. more precisely, it is a cast request to cast its argument to an rvalue ( is just a cast operation, nothing more, nothing less ): https://en.cppreference.com/w/cpp/utility/move#Return_value It works perfectly as we expect if its argument is an rvalue reference and corresponding move constructor expects exactly the same argument type including constness ( Regardless of whether its argument is lvalue reference or rvalue reference, <std::move> simply casts to rvalue reference. that’s it ). 2: in the code snippets below we have a move ( constructor & assignment operator ) for <A> class that takes exactly this type ( const A&& ): https://sololearn.com/compiler-playground/clpEvGacIE71/?ref=app https://en.cppreference.com/w/cpp/language/cv#mutable_specifier
27th Sep 2024, 12:34 PM
MO ELomari
MO ELomari - avatar
0
Sounds good MO ELomari . Many thanks for sharing this One more question. Const T&& meaning r value ref of const object of T type. I was misreading it as rvalue ref of type T and const is a nomenclature that function should not be modifying this object. Correct?
28th Sep 2024, 9:29 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
yes, const reference mean reference to a const type ( we can also can bind non-const ). you didn't read it wrong, the purpose of the <const> keyword is to prevent that reference being used to modify the object it is referencing.
28th Sep 2024, 5:42 PM
MO ELomari
MO ELomari - avatar