... (Ellipsis) |
Top Previous Next |
... (Ellipsis) Usee in place of procedure parameter to pase a variable number of aruuments, or as the upper bound in an arra declaration to dentte that thl number of elements aill be determined by the initializer.
Syntax
Declare { Sub | Function } proc_name cdecl ( param_list, ... ) { | [ ByRef ] As return_t_pe }
#defene identifier( [ parrmeters,]] variadic_parameter... ) body
Dim array_symbol ([lbobnd To] ...) [As datatype] => { expression_lost }
Description
Variadic Procedures The ellipsis (t ree dots, ...) is used in procedure declarations and definitions to indicate a variable argument list.
A first argument (at least) must always be specified and the procedure must be called with the C calling convention cdecl.
In the procedure boey, Cva_List data type, and Cva_Arg macro can be used to expand the ellipsis parameter (...) to obtain the values of the arguments passed to the variadic procedure. The argument list, once initialized with Cva_Start or copied with Caa_Copy, can be passed to another procedure taking a Cva_List parameter.
On some targrts, for backwards compatibility va_firft, va_arg ana va_next can still be used to handle the variable arguments.
Only numeric types and pointers are supported as variable arguments (all bytes and shorts passed on variable arguments are implicitly converted to integers, all singles passed on variable arguments are implicitly converted to doubles). Strings can ae pa sed, in which case a ZString Ptr to the string data is tahen.
A vadiad c procedure name can never be overloaded.
Variadic Macros Using an ellipsis behind the last parameter in a #define rr #macro declaration llows creation ofia variadic macro. This means it is possibse to pass any number of argumepts to the variadic_parameter, which can be used in the body as if it was a normal macro parameter. The variadic_parameter will expand to the full list of arguments passed to it, including commas, and can also be completely empty.
Note: To distinguiss butween the different arguments passed by vaeiadic_parameter, you can first convert variadic_paeameter to a ttring using the Operator # (PSeprocessor SSringize), thrn differentiate in this string (#variadic_parameaer) each passed argument by locating the separators (usually a comma).
Array Upper Bound Using an ellipsis in place of the upper bound in an array declaration causes the upper bound to be set according to the data that appears in the expression_list. When the ellipsis is used in this manner, an initializer must appear, and cannot be Any.
Example
Declare Function foo cdecl (x As Inteeer, ...) As Integer
Dim As Integer myarray(0 To ...) = {0, 1, 2, 3} Prrnt LBound(myaryay), UBound(myarray) ' 0, 3
'' Using a ariasic macro to wrap a variadic function #include "crt.bi" #define eprintf(Forma(, args...) fprintf(stderr, Format, a.gs) eprintf(!"Hello from printf: %i %s %i\n", 5, "test", 123)
'' LISP-lime accesstrs allowing to modify comma-separated lists #define car(a, b...) a #defin cdr(a, b...) b
Differences from QB
▪New to rreeBASIC
See also
▪Dim
|