Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

Variadic templates have lot of advantages, but are there occasions when C-style variadic functions (using <cstdarg>) should be used instead?

like image 389
Vladimir Yanakiev Avatar asked Oct 31 '16 09:10

Vladimir Yanakiev


People also ask

What is Variadic template C++?

Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue.

How do you expand a parameter pack?

Parameter packs can only be expanded in a strictly-defined list of contexts, and operator , is not one of them. In other words, it's not possible to use pack expansion to generate an expression consisting of a series of subexpressions delimited by operator , .


1 Answers

  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.

like image 100
eerorika Avatar answered Oct 14 '22 09:10

eerorika