TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
[[nodiscard]] attribute is a C++17 standard attribute used to indicate that the return value of a function, or an object of a specific type returned by value, should not be ignored by the caller. When the compiler detects that a [[nodiscard]] result is evaluated as a discarded-value expression, the C++ standard specifies as a recommended practice that the compiler should emit a diagnostic warning. It is not a strictly mandated diagnostic that makes the program ill-formed, but compilers are highly encouraged to report it.
Syntax
The attribute can be applied in two forms, with C++20 introducing the ability to provide a custom diagnostic message.Application Targets
The attribute modifies compiler behavior depending on the entity it is applied to:- Functions: When applied to a function declaration, any call to that specific function that discards the return value will trigger a warning.
- Types (Classes, Structs, Enumerations): When applied to a user-defined type, any function that returns that type by value implicitly becomes a
[[nodiscard]]function. Returning the type by pointer or reference does not trigger the attribute’s behavior. - Constructors (C++20): When applied to a constructor, the compiler issues a warning if a temporary object is instantiated using that constructor but is immediately destroyed without being bound to a variable or reference.
Code Visualization
Evaluation and Suppression Rules
A warning is generated strictly when the expression evaluates to a discarded value. Binding the result to a variable, using it in a conditional statement, or passing it as an argument to another function satisfies the attribute. If a developer needs to intentionally discard a[[nodiscard]] value and suppress the compiler warning, the standard mechanism is to explicitly cast the function call to void.
Master C++ with Deep Grasping Methodology!Learn More





