Vladimir Yanakiev Vladimir Yanakiev - 1 month ago 21
C++ Question

Is there a case where vararg functions should be preferred over variadic templates?

Variadic templates have lot of advantages, but are there occasions when C-style variadic functions (using

<cstdarg>
) should be used instead?

Answer
  1. If you provide a C API with C++ implementation, then templates are not an option for the API. Varargs are.

  2. If you need to support a compiler that doesn't support C++11 or newer standard, then variadic templates are not available. Varargs are.

  3. If you need a compilation firewall. I.e. you need to hide the implementation of the function from the header, then variadic template is not an option. Varargs are.

  4. On memory constrained systems (embedded), the different functions generated by the template may introduce too much bloat. That said, such systems are typically also real time, in which case varargs might also unacceptable due to branching and stack usage.