Telegram Group & Telegram Channel
std::move ничего не двигает 🤯

Вот типичная ошибка, которая встречается даже у опытных:


std::string foo() {
std::string s = "hello";
return std::move(s); //
}


Кажется, что std::move здесь «ускоряет» возврат. Но это зло. На самом деле, компилятор и без std::move применяет Return Value Optimization (RVO) и возвращает s без копирования. А вот std::move ломает RVO — теперь вызывается перемещающий конструктор, и компилятор не может это оптимизировать.

Результат:

* return s; — возможно, вообще без затрат (RVO).
* return std::move(s);гарантированно перемещение (дороже, чем RVO).

🔑 Правило: никогда не пиши std::move при возврате локальной переменной по значению. Доверься компилятору.

Когда std::move действительно нужен? Например:


void bar(std::string&& s) {
auto local = std::move(s); // перемещаем из rvalue-ссылки
}


Здесь всё логично: мы явно говорим, что хотим «украсть» содержимое.

Вывод: std::move — это не перемещение, а обещание, что объект можно обобрать. А перемещать будет уже компилятор.

➡️ @cpp_geek



tg-me.com/cpp_geek/317
Create:
Last Update:

std::move ничего не двигает 🤯

Вот типичная ошибка, которая встречается даже у опытных:


std::string foo() {
std::string s = "hello";
return std::move(s); //
}


Кажется, что std::move здесь «ускоряет» возврат. Но это зло. На самом деле, компилятор и без std::move применяет Return Value Optimization (RVO) и возвращает s без копирования. А вот std::move ломает RVO — теперь вызывается перемещающий конструктор, и компилятор не может это оптимизировать.

Результат:

* return s; — возможно, вообще без затрат (RVO).
* return std::move(s);гарантированно перемещение (дороже, чем RVO).

🔑 Правило: никогда не пиши std::move при возврате локальной переменной по значению. Доверься компилятору.

Когда std::move действительно нужен? Например:


void bar(std::string&& s) {
auto local = std::move(s); // перемещаем из rvalue-ссылки
}


Здесь всё логично: мы явно говорим, что хотим «украсть» содержимое.

Вывод: std::move — это не перемещение, а обещание, что объект можно обобрать. А перемещать будет уже компилятор.

➡️ @cpp_geek

BY C++ geek




Share with your friend now:
tg-me.com/cpp_geek/317

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

Telegram has exploded as a hub for cybercriminals looking to buy, sell and share stolen data and hacking tools, new research shows, as the messaging app emerges as an alternative to the dark web.An investigation by cyber intelligence group Cyberint, together with the Financial Times, found a ballooning network of hackers sharing data leaks on the popular messaging platform, sometimes in channels with tens of thousands of subscribers, lured by its ease of use and light-touch moderation.

Can I mute a Telegram group?

In recent times, Telegram has gained a lot of popularity because of the controversy over WhatsApp’s new privacy policy. In January 2021, Telegram was the most downloaded app worldwide and crossed 500 million monthly active users. And with so many active users on the app, people might get messages in bulk from a group or a channel that can be a little irritating. So to get rid of the same, you can mute groups, chats, and channels on Telegram just like WhatsApp. You can mute notifications for one hour, eight hours, or two days, or you can disable notifications forever.

telegram from us


Telegram C++ geek
FROM USA