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
1 answer

How can have an argument to a function be a type? (Similar to va_arg's second argument)

In the function va_arg for variadic functions, the second argument is just 'type'. When using this function, examples pass something like 'int'. How can I pass and use types in functions of my own? For example if I wanted to malloc a block of memory…
nevets
  • 11
  • 3
1
vote
2 answers

C++ variadic function syntax

In the C++03 standard, [dcl.fct] p.2 states that: The parameter-declaration-clause determines the arguments that can be specified, and their processing, when the func- tion is called. [ Note: the parameter-declaration-clause is used to convert the…
user42768
  • 1,951
  • 11
  • 22
1
vote
2 answers

C++ creating an interface that has variadic template methods

So basically I'm looking for a way to create an interface with various implementations for different platforms. Normally this would be quite straight forward, but I'm wanting to create an interface for a logger, and being able to have functions such…
Hex Crown
  • 753
  • 9
  • 22
1
vote
2 answers

Implementing a sub-function with va_list and va_arg

I have a varargs-style function that I want to split out to a va_list-style sub-function. The original function: void container_append(container_t *c, element_t *element, ...) { element_t *e; va_list ap; va_start(ap, element); while((e =…
fearless_fool
  • 33,645
  • 23
  • 135
  • 217
1
vote
1 answer

How can I call a function of all variadically inherited classes?

I'd be grateful to any template gurus out there for some help on this one. I'm using CRTP for a mixin class and would like to be able to pass arguments to a function of the derived class and have it call the inherited functions of all mixins and…
1
vote
1 answer

Variadic char array shows up incorrectly in nested function C++

I have two functions for a windows wrapper class, through which Im trying to pass a variadic argument list of char* arrays The first one is: bool OsInterface::AddDropdown(std::string menu_item_name, RECT v_location, int num_entries, ...) { bool…
Mich
  • 3,188
  • 4
  • 37
  • 85
1
vote
2 answers

expand c macro __VA_ARGS__ elements

I want to define a macro like this #define ASSERT_EXIST(error, ...) some-impl-here then we can call it like this ASSERT_EXIST(100, a, b, c) and it should be expand to this if (!a_exists) return error("a is missing", 100); if (!b_exists) return…
zhihuifan
  • 1,093
  • 2
  • 16
  • 30
1
vote
0 answers

Method with VarArgs invokes first from JSP

I have User class with overloaded methods: public boolean checkAccessAvoid(Role... groups) { ... } public boolean checkAccessAvoid(String string) { ... } Controller: @RequestMapping(method = RequestMethod.GET,…
I3rutt
  • 574
  • 4
  • 18
1
vote
1 answer

Separate different types of arguments from va_list

I am trying to write a macro which gets the information and sends that information to another function by splitting the orginal va_list into string and another va_list spawned from the original one. Below is my code. Call to macro /* Usage…
Jabez
  • 1,883
  • 6
  • 19
  • 18
1
vote
0 answers

In c++, how do I wrap a Variadic function while changing the first parameter

How do I go about correctly wrapping a Variadic function when I need to change the first parameter's type? For instance: void original_function(char* format, ...) { // Other Code Here } void wrapper(string format, ...) { va_list args; …
David
  • 1,648
  • 1
  • 16
  • 31
1
vote
0 answers

Augmenting variable argument list

Is there a way for a function to append to variable number of arguments it already received? I'm using __builtin_va* of libgcc for ARM to implement variable argument functions (typedef'ed appropriately) int wrapper(char *name, ...) { ... …
Jeenu
  • 2,109
  • 2
  • 20
  • 27
1
vote
1 answer

Kotlin vararg array of array of any

I have a method accepting a vararg of the form fun arrayOfArrays(vararg aoa: Array) { } Now, I have trouble understanding how to call this method, e.g. fun callArrayOfArrays() { arrayOfArrays(arrayOf(1), arrayOf(1)) // 0) works …
Matthias
  • 1,005
  • 7
  • 20
1
vote
1 answer

Error "Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) " in a variadic function

I have this method which supposed to take variable number of String arguments and put them in an NSMutableArray: - (id)testArray: (NSString*) question, ... { NSMutableArray* questionsArray = [[NSMutableArray alloc]…
Fady E
  • 346
  • 3
  • 16
1
vote
6 answers

C Variable Member List for structs, is this possible?

I have a question about structures having a "variable members list" similar to the "variable argument list" that we can define functions as having. I may sound stupid or completely off the line in terms of C language basics, but please correct me if…
Dan
  • 11
  • 2
1
vote
1 answer

Variadic function does not pass the first argument correctly

I have used variadic functions to wrap printf (or vprintf). The following code works except for making mistake on the first variadic argument to warning_printf. Also, placing the string directly will change the ASCII character but it does not fix it…
alex
  • 257
  • 2
  • 7
1 2 3
99
100