Why C variable argument functions are an abomination (and what to do about it)
a day ago
- #variable arguments
- #stdarg
- #C programming
- C's variable argument functions are considered outdated and problematic, stemming from the language's philosophy of 'Trust the programmer'.
- The stdarg API in C is klunky, lacks type safety, and makes it difficult to build on top of or edit variable argument lists.
- Common issues include no standard way to signify the end of arguments, no type safety, and inability to easily pass variable arguments between functions.
- Workarounds involve using macros to automate argument counting and termination, but these solutions are not foolproof and can be error-prone.
- A proposed solution involves creating a new layer of API that abstracts variable arguments into a structured format, improving flexibility and safety.
- The use of third-party libraries like libffi can help bridge the gap by providing a more flexible way to call variadic functions dynamically.
- Future enhancements to C should include a va_count() function and a __VA_COUNT__ macro to simplify working with variable arguments.
- A new, more explicit varargs implementation could improve type safety and composability without breaking existing ABI compatibility.