Telegram Group & Telegram Channel
Почему std::move может не сработать, как ты ожидал

Всё просто? Хочешь передать объект по move — вызываешь std::move(obj) и думаешь, что теперь точно будет перемещение. Но не всё так однозначно.


void foo(std::string s) {
std::string local = std::move(s);
}


Выглядит, будто s перемещается в local. Но на практике — нет. Здесь копирование. Почему?

s — это lvalue, несмотря на std::move в правой части. А значит, выбирается std::string конструктор копирования, если только он не удалён.

Чтобы реально переместить, нужно явно вызвать std::move:


std::string local = std::move(s); // ОК — move-конструктор


Но будь осторожен:


std::string getStr() {
std::string tmp = "hello";
return std::move(tmp); // Не всегда нужно!
}


Здесь std::move ломает RVO (Return Value Optimization). Компилятор мог бы вернуть tmp без перемещения, вообще без копий. А std::move мешает, заставляя делать move-конструктор.

Выводы:
std::move не двигает, он обещает, что ты больше не тронешь объект
– Будь осторожен с std::move в return
– Не забудь, что lvalue остаётся lvalue, даже если ты его "обернул" std::move

➡️ @cpp_geek



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

Почему std::move может не сработать, как ты ожидал

Всё просто? Хочешь передать объект по move — вызываешь std::move(obj) и думаешь, что теперь точно будет перемещение. Но не всё так однозначно.


void foo(std::string s) {
std::string local = std::move(s);
}


Выглядит, будто s перемещается в local. Но на практике — нет. Здесь копирование. Почему?

s — это lvalue, несмотря на std::move в правой части. А значит, выбирается std::string конструктор копирования, если только он не удалён.

Чтобы реально переместить, нужно явно вызвать std::move:


std::string local = std::move(s); // ОК — move-конструктор


Но будь осторожен:


std::string getStr() {
std::string tmp = "hello";
return std::move(tmp); // Не всегда нужно!
}


Здесь std::move ломает RVO (Return Value Optimization). Компилятор мог бы вернуть tmp без перемещения, вообще без копий. А std::move мешает, заставляя делать move-конструктор.

Выводы:
std::move не двигает, он обещает, что ты больше не тронешь объект
– Будь осторожен с std::move в return
– Не забудь, что lvalue остаётся lvalue, даже если ты его "обернул" std::move

➡️ @cpp_geek

BY C++ geek




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

View MORE
Open in Telegram


C geek Telegram | DID YOU KNOW?

Date: |

How To Find Channels On Telegram?

There are multiple ways you can search for Telegram channels. One of the methods is really logical and you should all know it by now. We’re talking about using Telegram’s native search option. Make sure to download Telegram from the official website or update it to the latest version, using this link. Once you’ve installed Telegram, you can simply open the app and use the search bar. Tap on the magnifier icon and search for a channel that might interest you (e.g. Marvel comics). Even though this is the easiest method for searching Telegram channels, it isn’t the best one. This method is limited because it shows you only a couple of results per search.

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.

C geek from sg


Telegram C++ geek
FROM USA