Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

Standard attributes: [[nodiscard]], [[maybe_unused]] and friends

Reference for standard C++ attributes: nodiscard, maybe_unused, fallthrough, deprecated, noreturn, likely, unlikely, assume and no_unique_address.

An attribute is a piece of standardized metadata written between double square brackets, [[name]], that you attach to a declaration or statement. Attributes give the compiler and other tools extra information: "warn if the caller ignores this result", "this variable may be unused on purpose", "this branch is rarely taken". Unlike most keywords they never change what a correct program means; a compiler may ignore any attribute, although the common ones are implemented by every major compiler.

Attributes can carry arguments, for example [[nodiscard("check the error code")]] or [[deprecated("use v2")]]. Unknown attributes in the standard namespace are errors, but unknown vendor attributes (such as [[gnu::always_inline]]) are ignored with a warning, which makes them safe to use portably.

C++
[[nodiscard]] int parse(const char* text);

void f([[maybe_unused]] int flags) {
    if (flags > 0) [[unlikely]] { /* rare path */ }
}

Try it

C++ nodiscard, maybe_unused, fallthrough, likely

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ assume and no_unique_address

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Read-only: attributes that intentionally produce warnings.

C++
[[deprecated("use load_v2")]] void load();
[[nodiscard]] int compute();

load();      // warning: 'load' is deprecated: use load_v2
compute();   // warning: ignoring return value of 'int compute()', declared with attribute 'nodiscard'

Syntax

C++
[[attribute]]                         // no arguments
[[attribute(args)]]                   // with arguments
[[vendor::attribute]]                 // namespaced, e.g. [[gnu::noinline]]
[[using vendor: a, b]]                // shared namespace
Attribute Applies to Meaning
[[nodiscard]] / [[nodiscard("reason")]] functions, classes, enums, constructors Warn if the result (or an object of that type) is ignored. C++17; reason string and constructors C++20.
[[maybe_unused]] variables, parameters, functions, types, enumerators Do not warn if the entity is unused. C++17.
[[fallthrough]]; a null statement before a case A switch fall-through is intentional. C++17.
[[noreturn]] functions The function never returns to its caller. C++11.
[[deprecated]] / [[deprecated("why")]] most declarations Warn on use. C++14.
[[likely]] / [[unlikely]] statements, labels Hint about which branch is hot. C++20.
[[no_unique_address]] non-static data members Let an empty member take no storage. C++20.
[[assume(expr)]]; a null statement Promise expr is true here. C++23.
[[carries_dependency]] parameters, functions Memory-order hint; rarely used. C++11.
[[indeterminate]] variables Mark an intentionally uninitialized object. C++26.

Errors / undefined behavior: if an [[assume(expr)]] expression would be false at run time, the behavior is undefined. A function declared [[noreturn]] that does return is undefined behavior. Everything else only affects diagnostics and optimization.

Usage notes

  • Put [[nodiscard]] on functions where ignoring the result is a bug: error codes, allocation, std::move-like pure functions. Do not spray it on everything.
  • Cast to void to silence it deliberately: (void)save();.
  • [[likely]] and [[unlikely]] are hints, not guarantees. Measure before adding them; modern CPUs and profile-guided optimization usually do better.
  • [[assume]] is dangerous. The compiler trusts it and may delete code that handles the "impossible" case. Use assertions in debug builds instead if you are unsure.
  • Where the attribute goes matters. [[nodiscard]] goes before the declaration; on a class it goes after class/struct: struct [[nodiscard]] Result {};.
  • Portable vendor attributes ([[gnu::cold]], [[msvc::no_unique_address]]) are ignored by compilers that do not know them, so test with __has_cpp_attribute(name) when it matters.
  • [[no_unique_address]] is ignored by the MSVC ABI under that spelling; use [[msvc::no_unique_address]] there.