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.
[[noreturn]] attribute is a C++11 standard attribute specifier used to explicitly declare that a function will never return control flow to its immediate caller. It serves as a strict semantic guarantee to the compiler, altering how the compiler constructs the Control Flow Graph (CFG) and performs static analysis.
Syntax
The attribute is placed at the beginning of a function declaration, preceding the return type.Compiler Mechanics and Optimizations
When the compiler encounters a call to a function marked with[[noreturn]], it applies several mechanical adjustments to the compilation process:
- Control Flow Graph Modification: The compiler marks the call site as a terminal node in the CFG. Any instructions sequentially following the call are flagged as unreachable.
- Dead Code Elimination (DCE): Because the compiler knows control will not resume after the call, it strips out subsequent code, reducing binary size.
- Stack Frame Optimization: The compiler may omit generating standard function epilogues (stack frame teardown) for the caller if the
[[noreturn]]function is the last operation in a specific branch. - Static Analysis Suppression: It suppresses specific compiler warnings, most notably
-Wreturn-type(“control reaches end of non-void function”), when a code path concludes with a[[noreturn]]call instead of a standardreturnstatement.
Rules and Constraints
- Undefined Behavior (UB): If a function marked with
[[noreturn]]violates its contract and actually returns to the caller (either by executing areturnstatement or reaching the closing brace}), the program exhibits Undefined Behavior. - Return Types: The attribute does not require the function to have a
voidreturn type. A function can be declared to returnintor any other type while still being[[noreturn]]. The return type is syntactically valid but practically ignored, as a value can never be passed back. - Type System Isolation:
[[noreturn]]is an attribute of the function’s declaration, not its type. It cannot be applied to function pointers,typedefs, orstd::functionsignatures.
Valid Control Flow Mechanisms
To satisfy the compiler contract without triggering Undefined Behavior, a[[noreturn]] function must prevent control flow from returning to the caller via mechanisms such as:
- Throwing a C++ exception.
- Calling a process-terminating standard library function (e.g.,
std::abort,std::exit,std::terminate). - Executing a non-local jump (e.g.,
std::longjmp). - Entering an infinite loop that contains observable behavior (such as I/O operations,
volatilememory accesses, or atomic/synchronization operations). Note: Under C++ forward progress guarantees, a trivial infinite loop (e.g.,while(true) {}) without observable behavior is Undefined Behavior. The compiler is permitted to optimize the empty loop away, which would cause the function to reach its closing brace, thereby violating the[[noreturn]]contract. - Executing a hardware-level trap or interrupt.
Master C++ with Deep Grasping Methodology!Learn More





