A type whose instances can be encoded, and appropriately passed, as elements of a C va_list.


You use this protocol to present a native Swift interface to a C “varargs” API. For example, a program can import a C API like the one defined here:

int c_api(int, va_list arguments)

To create a wrapper for the c_api function, write a function that takes CVarArg arguments, and then call the imported C function using the withVaList(_:_:) function:

func swiftAPI(_ x: Int, arguments: CVarArg...) -> Int {
    return withVaList(arguments) { c_api(x, $0) }

Swift only imports C variadic functions that use a va_list for their arguments. C functions that use the ... syntax for variadic arguments are not imported, and therefore can’t be called using CVarArg arguments.

If you need to pass an optional pointer as a CVarArg argument, use the Int(bitPattern:) initializer to interpret the optional pointer as an Int value, which has the same C variadic calling conventions as a pointer on all supported platforms.


Adopted By

See Also

C Variadic Functions

func withVaList<R>([CVarArg], (CVaListPointer) -> R) -> R

Invokes the given closure with a C va_list argument derived from the given array of arguments.

struct CVaListPointer

A wrapper around a C va_list pointer.

func getVaList([CVarArg]) -> CVaListPointer

Returns a CVaListPointer that is backed by autoreleased storage, built from the given array of arguments.