CPP_GEEK Telegram 349
std::move vs std::forward: когда и что использовать

Сегодня покажу вам коротко, чем отличаются std::move и std::forward.

- std::move(obj) — безусловно превращает объект в rvalue. После этого объект считается "пустым" для повторного использования (в рамках контракта move). Используем, когда мы точно хотим "забрать" ресурсы.

- std::forward<T>(obj) — условно делает rvalue, если изначально пришёл rvalue. То есть это "perfect forwarding" для шаблонных функций.

Пример:


#include <utility>
#include <string>
#include <iostream>

template <typename T>
void wrapper(T&& arg) {
process(std::forward<T>(arg)); // сохраняет rvalue/lvalue-семантику
}

void process(const std::string& s) { std::cout << "Lvalue: " << s << '\n'; }
void process(std::string&& s) { std::cout << "Rvalue: " << s << '\n'; }

int main() {
std::string str = "Hello";
wrapper(str); // Lvalue
wrapper(std::move(str)); // Rvalue
}


Запомните:

- std::move - "забрать".
- std::forward - "передать как есть".

➡️ @cpp_geek
🔥5🥴4



tgoop.com/cpp_geek/349
Create:
Last Update:

std::move vs std::forward: когда и что использовать

Сегодня покажу вам коротко, чем отличаются std::move и std::forward.

- std::move(obj) — безусловно превращает объект в rvalue. После этого объект считается "пустым" для повторного использования (в рамках контракта move). Используем, когда мы точно хотим "забрать" ресурсы.

- std::forward<T>(obj) — условно делает rvalue, если изначально пришёл rvalue. То есть это "perfect forwarding" для шаблонных функций.

Пример:


#include <utility>
#include <string>
#include <iostream>

template <typename T>
void wrapper(T&& arg) {
process(std::forward<T>(arg)); // сохраняет rvalue/lvalue-семантику
}

void process(const std::string& s) { std::cout << "Lvalue: " << s << '\n'; }
void process(std::string&& s) { std::cout << "Rvalue: " << s << '\n'; }

int main() {
std::string str = "Hello";
wrapper(str); // Lvalue
wrapper(std::move(str)); // Rvalue
}


Запомните:

- std::move - "забрать".
- std::forward - "передать как есть".

➡️ @cpp_geek

BY C++ geek


Share with your friend now:
tgoop.com/cpp_geek/349

View MORE
Open in Telegram


Telegram News

Date: |

Other crimes that the SUCK Channel incited under Ng’s watch included using corrosive chemicals to make explosives and causing grievous bodily harm with intent. The court also found Ng responsible for calling on people to assist protesters who clashed violently with police at several universities in November 2019. Joined by Telegram's representative in Brazil, Alan Campos, Perekopsky noted the platform was unable to cater to some of the TSE requests due to the company's operational setup. But Perekopsky added that these requests could be studied for future implementation. Channel login must contain 5-32 characters Telegram Android app: Open the chats list, click the menu icon and select “New Channel.” Commenting about the court's concerns about the spread of false information related to the elections, Minister Fachin noted Brazil is "facing circumstances that could put Brazil's democracy at risk." During the meeting, the information technology secretary at the TSE, Julio Valente, put forward a list of requests the court believes will disinformation.
from us


Telegram C++ geek
FROM American