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


C geek Telegram | DID YOU KNOW?

Date: |

Telegram and Signal Havens for Right-Wing Extremists

Since the violent storming of Capitol Hill and subsequent ban of former U.S. President Donald Trump from Facebook and Twitter, the removal of Parler from Amazon’s servers, and the de-platforming of incendiary right-wing content, messaging services Telegram and Signal have seen a deluge of new users. In January alone, Telegram reported 90 million new accounts. Its founder, Pavel Durov, described this as “the largest digital migration in human history.” Signal reportedly doubled its user base to 40 million people and became the most downloaded app in 70 countries. The two services rely on encryption to protect the privacy of user communication, which has made them popular with protesters seeking to conceal their identities against repressive governments in places like Belarus, Hong Kong, and Iran. But the same encryption technology has also made them a favored communication tool for criminals and terrorist groups, including al Qaeda and the Islamic State.

Telegram announces Search Filters

With the help of the Search Filters option, users can now filter search results by type. They can do that by using the new tabs: Media, Links, Files and others. Searches can be done based on the particular time period like by typing in the date or even “Yesterday”. If users type in the name of a person, group, channel or bot, an extra filter will be applied to the searches.

C geek from sa


Telegram C++ geek
FROM USA