Defer Available in GCC and Clang
9 days ago
- #compiler features
- #resource management
- #C programming
- A technical specification (TS 25755) for defer is complete and moving through ISO’s publication procedures.
- Both gcc and clang communities are integrating the defer feature into their C implementations.
- Clang’s defer feature is available starting with clang-22.
- Developers are encouraged to switch to defer for cleanup handling to avoid resource leaks and simplify code.
- A fallback implementation for gcc (versions 9 and above) and clang (version 22 and above) is provided.
- The gcc fallback uses nested functions but does not produce hidden functions or trampolines in the executable.
- Older clang versions lack a fallback due to incompatible semantics with their 'blocks' extension.
- Examples show defer usage for managing memory allocation and mutex unlocking.
- Defer must be used with curly braces for compatibility with the gcc fallback.
- The defer feature does not require specific software infrastructure post-compilation.