-
C only gained
[[...]]attribute syntax in C23.
[[nodiscard]] (C23, C++17)
-
Is an attribute that tells the compiler to warn if the return value of a function (or certain types) is ignored.
[[nodiscard]] int compute();
void f() {
compute(); // warning: return value discarded
}
-
Applicable to:
-
functions
-
constructors
-
class/struct/enum types
-
-
In C:
-
Not available in older C standards (C11, C17, etc.)
-
Standardized in: C23
-
-
In C++:
-
Standardized in: C++17
[[nodiscard]] int parse(); -
Enhanced in: C++20 (added message support)
[[nodiscard("check the error code")]] int parse();
-
-
Old alternatives
-
GCC / Clang
__attribute__((warn_unused_result)) int compute(void); -
MSVC
_Check_return_ int compute(void);
-
[[maybe_unused]] (C23, C++17)
-
Suppresses unused warnings explicitly, documenting intent.
[[maybe_unused]] int debug_flag;
[[fallthrough]] (C23, C++17)
-
Makes switch fallthrough explicit and warning-free.
switch (x) {
case 1:
do_something();
[[fallthrough]];
case 2:
do_other();
}
-
Prevents accidental fallthrough bugs.
[[deprecated]] (C23, C++14)
-
Marks APIs as obsolete.
[[deprecated("Use new_api instead")]]
void old_api();
[[likely]] / [[unlikely]] (C++20)
-
Branch prediction hints.
if (ptr == nullptr) [[unlikely]] {
return;
}
-
Primary value: document intent, not performance .
-
Vendor alternative (both languages):
-
__builtin_expect(GCC/Clang)
-
[[carries_dependency]] (C++11)
-
Used with memory ordering / dependency chains (rarely used in practice).
-
It exists solely for a now largely abandoned optimization strategy:
memory_order_consumedependency chains. -
It tells the compiler: A value carries a dependency from an atomic load performed with memory_order_consume.
-
This was meant to allow weaker-than-acquire synchronization on weakly ordered CPUs (e.g., ARM, PowerPC).
-
considered failed design.
-
There have been ongoing efforts in the standards committee to redefine or replace
memory_order_consume.
void f(int* [[carries_dependency]] p);
-
Mostly relevant for low-level lock-free code.
-
Performance impact:
-
In theory:
-
allows weaker barriers than memory_order_acquire
-
avoids full memory fence
-
improves performance on weak memory architectures
-
-
In practice (2026):
-
No major compiler implements true consume semantics.
-
They upgrade consume -> acquire.
-
[[carries_dependency]] has effectively no performance impact.
-
-