Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

A variadic function – a function declared with a parameter list ending with ellipsis (...) – can accept a varying number of arguments of varying differing types. Variadic functions are flexible, but they are also hazardous. The compiler can't verify that a given call to a variadic function passes an appropriate number of arguments or that those arguments have appropriate types. Consequently, a runtime call to a variadic function that passes inappropriate arguments yields undefined behavior. Such undefined behavior could be exploited to run arbitrary code.

...

When a function call expression appears in some contexts, notably as the argument in a sizeof expression, the compiler performs overload resolution to determine the result type of the call, but the object code doesn't execute the call at runtime. In such cases, the compiler uses only the function's declaration, not its definition.

Some template metaprogramming techniques that exploit SFINAE (implement "substitution failure is not an error" (SFINAE) use variadic functions to implement compile-time type queries, as in:

...

In this example, is_ptr(e) returns true if expression e has a pointer type. is_ptr(e) calls variadic function isPtr(...), but the call takes place in a sizeof expression. ThusConsequently, isPtr(...) must be declared, but it need not, and should not, be defined.

...