> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C++ Nodiscard Attribute

The `[[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.

```cpp theme={"dark"}
// C++17 standard syntax
[[nodiscard]] return_type function_name();

// C++20 syntax with a string literal diagnostic message
[[nodiscard("string_literal")]] return_type function_name();
```

## 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

```cpp theme={"dark"}
// 1. Applied to a function
[[nodiscard]] int calculate_offset();

// 2. Applied to a type
struct [[nodiscard]] StatusToken {
    int code;
};

// Implicitly inherits [[nodiscard]] behavior because it returns StatusToken by value
StatusToken generate_token(); 

// Does NOT inherit [[nodiscard]] behavior (returns by reference)
StatusToken& get_cached_token();

// 3. Applied to a constructor (C++20)
struct Resource {
    [[nodiscard]] Resource(int size);
};
```

## 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`.

```cpp theme={"dark"}
void evaluate_nodiscard() {
    calculate_offset();         // Warning: ignoring return value of function declared with 'nodiscard' attribute
    int x = calculate_offset(); // OK: value is bound to 'x'

    generate_token();           // Warning: ignoring return value of type 'StatusToken' declared with 'nodiscard' attribute
    get_cached_token();         // OK: returns by reference, attribute does not apply

    Resource(1024);             // Warning: ignoring temporary object created by 'nodiscard' constructor

    // Explicit suppression
    (void)calculate_offset();   // OK: explicit cast to void suppresses the diagnostic
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
