Telegram Group & Telegram Channel
#creepy и #video

Тупое нововведение в C++23: копирование с помощью auto{}

Что нужно сделать, чтобы скопировать объект, особенно в шаблонном коде? Зная, что объявление переменной с типом auto это всегда копия, можно сделать так:
void func(const auto& something) {
auto copy = something;
use(copy);
}

Что, если мы не хотим объявлять новый объект, а создать копию "на месте"? Тогда можно сделать так (decay нужен потому что исходный тип может быть ссылочным и/или иметь cv-квалификаторы):
void func(const auto& something) {
use(std::decay_t<decltype(something)>{copy});
}

А что, если мы считаем что C++ еще недостаточно сложный? То в С++23 теперь можно делать так:
void func(const auto& something) {
use(auto{copy});
}

Эту информацию я узнал из канала "C++ Weekly": https://www.youtube.com/watch?v=5zVQ50LEnuQ В видео приводится не очень убедительный "мотивационный пример".

Комментарии к видео интереснее самого видео, все люди ловят фейспалм и пишут комментарии как "в чем вообще проблема копировать как раньше", "почему не сделали в виде std::copy", и прочее. В этом я с ними согласен!



group-telegram.com/cxx95/32
Create:
Last Update:

#creepy и #video

Тупое нововведение в C++23: копирование с помощью auto{}

Что нужно сделать, чтобы скопировать объект, особенно в шаблонном коде? Зная, что объявление переменной с типом auto это всегда копия, можно сделать так:

void func(const auto& something) {
auto copy = something;
use(copy);
}

Что, если мы не хотим объявлять новый объект, а создать копию "на месте"? Тогда можно сделать так (decay нужен потому что исходный тип может быть ссылочным и/или иметь cv-квалификаторы):
void func(const auto& something) {
use(std::decay_t<decltype(something)>{copy});
}

А что, если мы считаем что C++ еще недостаточно сложный? То в С++23 теперь можно делать так:
void func(const auto& something) {
use(auto{copy});
}

Эту информацию я узнал из канала "C++ Weekly": https://www.youtube.com/watch?v=5zVQ50LEnuQ В видео приводится не очень убедительный "мотивационный пример".

Комментарии к видео интереснее самого видео, все люди ловят фейспалм и пишут комментарии как "в чем вообще проблема копировать как раньше", "почему не сделали в виде std::copy", и прочее. В этом я с ними согласен!

BY C++95




Share with your friend now:
group-telegram.com/cxx95/32

View MORE
Open in Telegram


Telegram | DID YOU KNOW?

Date: |

Since January 2022, the SC has received a total of 47 complaints and enquiries on illegal investment schemes promoted through Telegram. These fraudulent schemes offer non-existent investment opportunities, promising very attractive and risk-free returns within a short span of time. They commonly offer unrealistic returns of as high as 1,000% within 24 hours or even within a few hours. Russian President Vladimir Putin launched Russia's invasion of Ukraine in the early-morning hours of February 24, targeting several key cities with military strikes. Meanwhile, a completely redesigned attachment menu appears when sending multiple photos or vides. Users can tap "X selected" (X being the number of items) at the top of the panel to preview how the album will look in the chat when it's sent, as well as rearrange or remove selected media. Groups are also not fully encrypted, end-to-end. This includes private groups. Private groups cannot be seen by other Telegram users, but Telegram itself can see the groups and all of the communications that you have in them. All of the same risks and warnings about channels can be applied to groups. In addition, Telegram's architecture limits the ability to slow the spread of false information: the lack of a central public feed, and the fact that comments are easily disabled in channels, reduce the space for public pushback.
from us


Telegram C++95
FROM American