PETSc version 3.17.5
PETSC_NODISCARD
Mark the return value of a function as non-discardable
Notes
Hints to the compiler that the return value of a function must be captured. A diagnostic may
(but is not required) be emitted if the value is discarded. It is safe to use this in C
and C++ source files.
Fortran Notes
Not available in Fortran
Example Usage
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));
}
auto x = factorial(10); // OK, capturing return value
factorial(10); // Warning: ignoring return value of function declared 'nodiscard'
auto f = Foo(x); // OK, capturing constructed object
Foo(x); // Warning: Ignoring temporary created by a constructor declared 'nodiscard'
Developer Notes
It is highly recommended if not downright required that any PETSc routines written in C++
returning a PetscErrorCode be marked PETSC_NODISCARD. Ignoring the return value of PETSc
routines is not supported; unhandled errors may leave PETSc in an unrecoverable state.
See Also
PETSC_NULLPTR, PETSC_CONSTEXPR_14
Level
beginner
Location
include/petscmacros.h
Index of all Sys routines
Table of Contents for all manual pages
Index of all manual pages