Telegram Group & Telegram Channel
#creepy

Как обмануть [[nodiscard]] через std::ignore 💩

В C++17 добавили атрибут [[nodiscard]], которым можно помечать функции, чтобы тот, кто вызвал функцию, не игнорировал возвращаемое значение.

Во многих окружениях любой warning ломает компиляцию (с флагом -Werror). Можно ли все-таки проигнорировать значение?

Оказывается - да 😀 В C++17 добавили std::ignore - это объект, которому можно присвоить любое значение, без эффекта.

[[nodiscard]] int status_code() { return -1; }
[[nodiscard]] std::string sample_text() { return "hello world"; }

void foo() {
std::ignore = status_code(); // нет warning/error
std::ignore = sample_text(); // нет warning/error
}

Но в духе C++ будет запретить std::ignore для некоторых типов. Покопавшись в реализации, можно выключить его, например для int:
template<>
const decltype(std::ignore)&
decltype(std::ignore)::operator=(const int&) const = delete;

Пример на godbolt 👦
Please open Telegram to view this post
VIEW IN TELEGRAM



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

#creepy

Как обмануть [[nodiscard]] через std::ignore 💩

В C++17 добавили атрибут [[nodiscard]], которым можно помечать функции, чтобы тот, кто вызвал функцию, не игнорировал возвращаемое значение.

Во многих окружениях любой warning ломает компиляцию (с флагом -Werror). Можно ли все-таки проигнорировать значение?

Оказывается - да 😀 В C++17 добавили std::ignore - это объект, которому можно присвоить любое значение, без эффекта.

[[nodiscard]] int status_code() { return -1; }
[[nodiscard]] std::string sample_text() { return "hello world"; }

void foo() {
std::ignore = status_code(); // нет warning/error
std::ignore = sample_text(); // нет warning/error
}

Но в духе C++ будет запретить std::ignore для некоторых типов. Покопавшись в реализации, можно выключить его, например для int:
template<>
const decltype(std::ignore)&
decltype(std::ignore)::operator=(const int&) const = delete;

Пример на godbolt 👦

BY C++95


Warning: Undefined variable $i in /var/www/group-telegram/post.php on line 260

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

View MORE
Open in Telegram


Telegram | DID YOU KNOW?

Date: |

Telegram was co-founded by Pavel and Nikolai Durov, the brothers who had previously created VKontakte. VK is Russia’s equivalent of Facebook, a social network used for public and private messaging, audio and video sharing as well as online gaming. In January, SimpleWeb reported that VK was Russia’s fourth most-visited website, after Yandex, YouTube and Google’s Russian-language homepage. In 2016, Forbes’ Michael Solomon described Pavel Durov (pictured, below) as the “Mark Zuckerberg of Russia.” READ MORE Pavel Durov, Telegram's CEO, is known as "the Russian Mark Zuckerberg," for co-founding VKontakte, which is Russian for "in touch," a Facebook imitator that became the country's most popular social networking site. Anastasia Vlasova/Getty Images "Russians are really disconnected from the reality of what happening to their country," Andrey said. "So Telegram has become essential for understanding what's going on to the Russian-speaking world."
from us


Telegram C++95
FROM American