CPLUSPLUC Telegram 1122
🧠 C++ Задача для продвинутых: загадка с `std::move` и `const`

Задача: что выведет этот код и почему?


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

std::string identity(std::string&& s) {
std::cout << "rvalue reference\n";
return std::move(s);
}

std::string identity(const std::string& s) {
std::cout << "const lvalue reference\n";
return s;
}

int main() {
const std::string a = "hello";
std::string b = identity(std::move(a));
std::cout << "Result: " << b << std::endl;
}


🔍 Варианты ответов:
• a) rvalue reference
• b) const lvalue reference
• c) Ошибка компиляции
• d) Поведение зависит от компилятора

💡 Разбор:

a объявлена как const std::string. Даже после std::move(a) она остаётся const, потому что std::move не снимает константность, он лишь преобразует к rvalue:


std::move(const std::string&) → const std::string&&


Смотрим на перегрузки identity:

identity(std::string&&) — принимает неконстантный rvalue
identity(const std::string&) — принимает константный lvalue

const std::string&& не подходит к std::string&&, потому что нельзя привязать rvalue-ссылку к `const`-объекту без соответствия типов.

👉 Вызовется вторая функция, с const std::string&

Ответ: b) const lvalue reference

🧠 Вывод: std::move не делает объект мутабельным. Если объект const, он остаётся const, и rvalue-перегрузки не срабатывают.

📌 Совет: перед использованием std::move всегда учитывайте `const`-квалификатор. Он может "сломать" ожидаемую семантику перемещения.



tgoop.com/cpluspluc/1122
Create:
Last Update:

🧠 C++ Задача для продвинутых: загадка с `std::move` и `const`

Задача: что выведет этот код и почему?


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

std::string identity(std::string&& s) {
std::cout << "rvalue reference\n";
return std::move(s);
}

std::string identity(const std::string& s) {
std::cout << "const lvalue reference\n";
return s;
}

int main() {
const std::string a = "hello";
std::string b = identity(std::move(a));
std::cout << "Result: " << b << std::endl;
}


🔍 Варианты ответов:
• a) rvalue reference
• b) const lvalue reference
• c) Ошибка компиляции
• d) Поведение зависит от компилятора

💡 Разбор:

a объявлена как const std::string. Даже после std::move(a) она остаётся const, потому что std::move не снимает константность, он лишь преобразует к rvalue:


std::move(const std::string&) → const std::string&&


Смотрим на перегрузки identity:

identity(std::string&&) — принимает неконстантный rvalue
identity(const std::string&) — принимает константный lvalue

const std::string&& не подходит к std::string&&, потому что нельзя привязать rvalue-ссылку к `const`-объекту без соответствия типов.

👉 Вызовется вторая функция, с const std::string&

Ответ: b) const lvalue reference

🧠 Вывод: std::move не делает объект мутабельным. Если объект const, он остаётся const, и rvalue-перегрузки не срабатывают.

📌 Совет: перед использованием std::move всегда учитывайте `const`-квалификатор. Он может "сломать" ожидаемую семантику перемещения.

BY C++ Academy


Share with your friend now:
tgoop.com/cpluspluc/1122

View MORE
Open in Telegram


Telegram News

Date: |

How to Create a Private or Public Channel on Telegram? Telegram desktop app: In the upper left corner, click the Menu icon (the one with three lines). Select “New Channel” from the drop-down menu. bank east asia october 20 kowloon Matt Hussey, editorial director of NEAR Protocol (and former editor-in-chief of Decrypt) responded to the news of the Telegram group with “#meIRL.” Image: Telegram.
from us


Telegram C++ Academy
FROM American