:orphan: # PETSC_NODISCARD Mark the return value of a function as non-discardable Not available in Fortran ## Notes Hints to the compiler that the return value of a function must be captured. A diagnostic may (but is not required to) be emitted if the value is discarded. It is safe to use this in both C and C++ source files. In this context "captured" means assigning the return value of a function to a named variable or casting it to `void`. Between the two, assigning to a named variable is the most portable way of silencing any warnings, since `PETSC_NODISCARD` may expand to GCC's `__attribute__((warn_unused_result))` which will still emit warnings when casting results to `void`. ## Example Usage ```none class Foo { int x; public: PETSC_NODISCARD Foo(int y) : x(y) { } }; PETSC_NODISCARD int factorial(int n) { return n <= 1 ? 1 : (n * factorial(n - 1)); } factorial(10); // Warning: ignoring return value of function declared 'nodiscard' auto x = factorial(10); // OK, capturing return value (void)factorial(10); // Maybe OK, casting to void auto y = factorial(10); // OK, capturing in y (and casting y to void to silence (void)y; // set-but-not-used warnings) Foo(x); // Warning: Ignoring temporary created by a constructor declared 'nodiscard' auto f = Foo(x); // OK, capturing constructed object (void)Foo(x); // Maybe OK, casting to void auto g = Foo(x); // OK, capturing in g (and casting g to void to silence set-but-not-used (void)g; // warnings) ``` ## See Also `PETSC_NULLPTR`, `PETSC_CONSTEXPR_14` ## Level beginner ## Location include/petscmacros.h --- [Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/include/petscmacros.h) [Index of all Sys routines](index.md) [Table of Contents for all manual pages](/manualpages/index.md) [Index of all manual pages](/manualpages/singleindex.md)