14.1 Template declarations

14.1.1 Use variadic templates rather than an ellipsis

Use of the ellipsis notation … to indicate an unspecified number of arguments should be avoided. Variadic templates offer a type-safe alternative.

#include <iostream>
               
int print ()
{
std::cout << std::endl;
}
                
// @@+ Compliant: variadic template function +@@
template
void print (First const & v, Rest const & ... args)
{
std::cout << v;
print (args ...); // recursive template instantiation or a call to print ()
}
               
void foo (int32_t i)
{
print ("And only ", i, " little ducks came back.");
}

If use of a variadic template is not possible, function overloading or function call chaining (e.g. similar to stream output) should be considered.

References

  • HIC++ v3.3 - 11.6
  • JSF AV C++ Rev C - 108
  • MISRA C++:2008 - 8-4-1

Click here for references