Variadic arguments allow a function to accept any number of arguments (in this rule, we are not talking about variadic templates, but about
functions with ellipses). But these arguments have to respect some criteria to be handled properly.
The standard imposes some requirements on the class types that can be passed as variadic arguments, and those requirements vary according to the
C++ standard version in use:
- Before C++11, the standard only allows POD types to be used as variadic arguments.
- In C++11, the rules are relaxed such that any class type with an eligible non-trivial copy constructor, an eligible non-trivial move
constructor, or a non-trivial destructor can be used in variadic arguments.
The rule detects any violations of these requirements since they can trigger undefined behavior.
Additionally, since using an incorrect type to access the passed parameter within the variadic function can lead to undefined behavior, the rule
goes a step further and reports all cases when class types are passed as variadic arguments. The rationale is that, most likely, the user forgot to
call a method on the object being passed (std::string_view::data()
for example) that would get a member of a built-in type.
When in need to pass class types to functions that take a variable number of arguments, consider using modern type-safe alternatives like C++11
parameter packs instead of variadic functions.
Noncompliant code example
void my_log(const char* format, ...);
void f() {
std::string someStr = "foo";
my_log("%s", someStr); // Noncompliant; the C++11 standard requires types passed as variadic arguments to have a trivial copy constructor. The user probably meant to pass someStr.c_str() here
std::string_view someStrView = "bar";
my_log("%s", someStrView); // Noncompliant; the user probably meant to pass someText.data()
std::chrono::duration<float> duration;
my_log("%f", duration); // Noncompliant, the user probably meant to pass duration.count()
}
Compliant solution
void my_log(const char* format, ...);
void f() {
std::string someStr = "foo";
my_log("%s", someStr.c_str()); // Compliant
std::string_view someStrView = "bar";
my_log("%s", someStrView.data()); // Compliant
std::chrono::duration<float> duration;
my_log("%f", duration.count()); // Compliant
}
Exceptions
The rule doesn’t report an issue in the following cases:
- When the called variadic function doesn’t have any non-variadic parameters. This is a common pattern when the function is used as a catch-all
net for an overload set. This is also guaranteed to be safe since there is no portable to access the passed arguments.
- When the called variadic function is known to accept a class type object as a variadic argument (e.g., the
semctl
system call).
// This variadic function is used as a catch-all net to terminate recursion
std::size_t elementsCount(...) { return 1u; }
template<typename T>
std::size_t elementsCount(const std::vector<T>& vec) {
// Sum the elements of all nested vectors recursively
return std::accumulate(vec.begin(), vec.end(), 0u, [] (const std::size_t count, const T& element) {
return count + elementsCount(element); // Compliant (the callee doesn't have non-variadic arguments)
});
}