The [[nodiscard]] attribute was added in C++17. It is used to mark functions so that whoever called the function does not ignore the return value.
In many environments, any warning breaks compilation (for example, when using the -Werror
flag). So is it still possible to ignore the returned value?
It turns out possible! They also have std::ignore in C++. This is an object that can be assigned any value, without effect.
1
2
3
4
5
6
7
[[nodiscard]] int status_code() { return -1; }
[[nodiscard]] std::string sample_text() { return "hello world"; }
void foo() {
std::ignore = status_code(); // no warning/error
std::ignore = sample_text(); // no warning/error
}
But in the chaotic spirit of C++, we can prohibit std::ignore
for some types. After digging into the implementation of the standard library, you can do this, for example for the int
type:
1
2
3
template<>
const decltype(std::ignore)&
decltype(std::ignore)::operator=(const int&) const = delete;