Nodiscard is a C++ attribute that makes it so the return type of something must be used.
Imagine that you call a function for its side-effects, but it returns a bool for success/failure.
bool do_something() {
auto worked_out = do_something_else();
if (worked_out) {
return true;
} else {
return false;
}
}
Without [[nodiscard]]
, the caller gets no warning if
they discard the returned type:
(); // this is fine. do_something
With nodiscard, however:
[[nodiscard]] bool do_something() {
auto worked_out = do_something_else();
if (worked_out) {
return true;
} else {
return false;
}
}
(); // this generates a compiler error
do_somethingauto res = do_something(); // this does not.