The [[nodiscard]]
attribute in C++ is a hint to the compiler that the return value of a function should not be ignored. If the return value of a function decorated with [[nodiscard]]
is ignored, the compiler will generate a warning. This is useful for indicating that the function performs some computation whose result should be used, or that the function reports a status that should be checked.
Here are some situations where [[nodiscard]]
could be beneficial:
Functions that return error codes should typically not have their return values ignored, because doing so would mean that the error goes unchecked.
[[nodiscard]] int performOperation() {
// ... do something and return an error code
return -1; // example error code
}
int main() {
performOperation(); // Compiler warning: ignoring return value of ‘int performOperation()’, declared with attribute nodiscard [-Wunused-result]
return 0;
}
For functions that return some resource that must be released or closed, using [[nodiscard]]
can remind the developer to store the result and manage the resource appropriately.
[[nodiscard]] FILE* openFile(const char* fileName) {
return fopen(fileName, "r");
}
Functions that perform checks or validations are good candidates for [[nodiscard]]
to ensure that their results are actually used in a condition.
[[nodiscard]] bool isValid(int x) {
return x > 0;
}
Functions that perform computations are often expected to have their results used in further computations. Ignoring the result usually indicates a programmer error.
[[nodiscard]] int add(int x, int y) {
return x + y;
}
- Improved Code Quality: Forces developers to handle results or errors, leading to more robust code.
- Self-Documenting Code: Indicates the intent that the return value is significant.
- Easier Debugging: Helps to catch instances where a return value is accidentally ignored, which can be a source of subtle bugs.
The [[nodiscard]]
attribute is a part of modern C++ and was introduced in C++17. It's a part of the language's move toward enabling more robust and self-checking code.