Questions tagged [variadic-functions]

A function is variadic if it can accept a variable number of arguments; that is, its arity is not fixed. The abbreviation "varargs" is commonly used to refer to these types of functions.

Variadic functions are functions which accept a variable number of arguments—for example, a function that may be called with either 1 or 2 arguments.

Most high level languages have support for variadic functions within the language's syntax proper. Some of these languages do not require extra syntax (for example, JavaScript, Perl, and PHP). They typically access variable arguments through some sort of default variable (Perl uses _@).

Most compiled languages (for example, D, Java, C#, and VB.NET) and even some interpreted languages (for example, Python, CoffeeScript, Ruby, and Scheme) require explicit syntax in order to use variadic functions. They typically "roll" all excess arguments into a specially noted argument which can be accessed like an array (Python uses the *args syntax, where *args is a tuple object).

C and C++ variadic functions use the standard library (<stdarg.h>, C++ alternatively <cstdarg>).
They use the va_* functions in order to unpack and access variable numbers of arguments. See man stdarg or ANSI section 2.10 for more information on these functions.

2645 questions
1
vote
2 answers

Converting "String*" (Scala) to/from "String..." (Java)

I'm converting some of the Elasticsearch Java API to Scala. There are some methods in Java that take a variable of type String... indices, so I'm trying to provide a Scala function with a variable of type indices: String*, that encapsulates that…
1
vote
2 answers

Is there an analogous function to `vsnprintf` that works with `std::string`

I don't see a constructor for std::string that can consume a va_list. Is there a common solution for converting a va_list to a std::string? I've seen solutions in the form of: std::string vstring (const char * format, ...) { std::string result; …
Zak
  • 12,213
  • 21
  • 59
  • 105
1
vote
1 answer

maxima: shorthanding the list of arguments to a function

Suppose I have a bunch of functions such as g(x0,x1,x2,x3,x4) := x0+x1+x2+x3+x4+x5. I would like to use them in other expressions, but explicitly enumerating all the variables x0,...,x4 is cumbersome. How to shorthand it? I tried something like vars…
Ilonpilaaja
  • 1,169
  • 2
  • 15
  • 26
1
vote
2 answers

Passing all members of a case class to a Java method taking variadic arguments

Let's say I have a case class CC(a: Int, b: String, c: Double). For simplicity sake I'll limit it to 3 parameters but imagine I have many more, say 20. My question is really about the larger example. Now I have a Java SQLServerDataTable object…
gknauth
  • 2,310
  • 2
  • 29
  • 44
1
vote
0 answers

gcc error message passing a pointer to va_list

I can not understand the error I get from gcc for the following source: #include #include static void variable_arguments2(int n, va_list *ap) { for(int i=0; i
mastupristi
  • 1,240
  • 1
  • 13
  • 29
1
vote
1 answer

variadic functions with `fcn( char *, ...)` how does it know when to end?

I've been reviewing stuff from multiple sources including my old favorite K&R second ed. I've been looking at variadic functions, and almost all the tutorials I've seen use and int before the ellipses to determine the total number variable…
Manny_Mar
  • 398
  • 2
  • 13
1
vote
1 answer

How do I use GLADcallback?

I'm using glad to generate OpenGL bindings, and have generated a debug build which includes the following: // this symbol only exists if generated with the c-debug generator #define GLAD_DEBUG typedef void (* GLADcallback)(const char *name, void…
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
1
vote
2 answers

Best practise for a variable-length argument lists in mysqli bind_param

I'm looking for the best approach / pattern / idiom for handling a variable-length argument for the mysqli bind_param method. I've created a variadic function in PHP that is similar to mysqli_stmt::bind_param. Thus, the first required argument…
Julian
  • 4,396
  • 5
  • 39
  • 51
1
vote
1 answer

Instantiate the type of the "rest" parameter of a variadic polymorphic procedure when arity is not known in advance in typed Racket

Let's say I want to transpose a 2 x n "matrix" (list of lists) mat. The idiomatic way of doing so in racket is (apply map list mat) To do the same thing in typed/racket, I have to help the type checker a little. The type of map in that case is (All…
1
vote
0 answers

Parameter Pack Iterating Backwards when evaluating function in argument list

I am learning parameter packing in C++ and am confused by the output of this snippet: template void f1(Ts... args) { std::cout << "------------------------" << std::endl; int dummy[sizeof...(Ts)] = { (std::cout << args <<…
superjax
  • 252
  • 2
  • 9
1
vote
1 answer

Weird behaviour when overloading varargs method

Usually methods with a fixed number of arguments are preferred over overloaded methods with a variable number of arguments. However, this example behaves differently: public class Main{ public static void main(String[] args){ print(1); …
Fabian Röling
  • 1,227
  • 1
  • 10
  • 28
1
vote
0 answers

Why I get "ambiguous" compile error overloading methods like f(int... arg) and f(Integer... arg)?

Why I get "ambiguous" compile error overloading methods like void f(int... arg) and void f(Integer... arg) ? I know that according to JLS argument resolution is done in three steps (proceeding to next step only if previous step yielded no match):…
caasdads
  • 409
  • 4
  • 12
1
vote
1 answer

How to set std::array size via parameter pack arguments?

I have an N-dimensional Matrix class that has a constructor with a parameter pack. Is it possible to set the size of the std::array member variable depending on the values in the parameter pack? As far as I understand the values in the parameter…
SPA
  • 77
  • 1
  • 4
1
vote
3 answers

C++ need a container to store user defined function

I am trying to create a interface between user defined function and data. Let's say I need to create a function called MapFun(), input of MapFun() includes user defined function (UDF) handle and UDF inputs. void userFun1(Data data, int in1, int…
J.Yang
  • 49
  • 7
1
vote
2 answers

Type Safety - A Generic Array is created for varargs parameter - Is this a good solution?

If I add columns to a JavaFX TableView: tableView.getColumns().addAll( col1, col2, col3 ); I get this warning: Type safety: A generic array of TableColumn< T, ? > is created for a varargs parameter If I manually put the vargs into a list, I don't…
Grumblesaurus
  • 3,021
  • 3
  • 31
  • 61
1 2 3
99
100