What guarantees and hazards come with C-style variadic functions using ellipsis and va_list, and when should variadic templates be preferred?
Show answer and explanation
An ellipsis parameter carries no count or runtime type metadata. Arguments undergo default promotions, such as float to double and narrow integral types to int or unsigned int, and each va_arg request must use the corresponding promoted type; a mismatch is generally undefined behavior. The callee needs an external protocol such as a count or sentinel, initializes traversal with va_start, uses va_copy for an independent traversal, and pairs every initialized list with va_end. Passing class types through this boundary also has restricted or implementation-defined cases, so it is a fragile interface. Variadic templates preserve the argument types, allow compile-time constraints and forwarding, and work with fold expressions, so they are the normal C++ choice. Keep C varargs only for required C ABI or legacy interoperability, validate the external protocol, and expose a typed C++ wrapper when possible.
An ellipsis parameter carries no count or runtime type metadata. Arguments undergo default promotions, such as float to double and narrow integral types to int or unsigned int, and each va_arg request must use the corresponding promoted type; a mismatch is generally undefined behavior. The callee needs an external protocol such as a count or sentinel, initializes traversal with va_start, uses va_copy for an independent traversal, and pairs every initialized list with va_end. Passing class types through this boundary also has restricted or implementation-defined cases, so it is a fragile interface. Variadic templates preserve the argument types, allow compile-time constraints and forwarding, and work with fold expressions, so they are the normal C++ choice. Keep C varargs only for required C ABI or legacy interoperability, validate the external protocol, and expose a typed C++ wrapper when possible.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.