Questions tagged [variadic]

In computer science, an operator or function is variadic if it can take a varying number of arguments; that is, if its arity is not fixed.

668 questions
361
votes
11 answers

Why use the params keyword?

I know this is a basic question, but I couldn't find an answer. Why use it? if you write a function or a method that's using it, when you remove it the code will still work perfectly, 100% as without it. E.g: With params: static public int…
MasterMastic
  • 20,711
  • 12
  • 68
  • 90
236
votes
13 answers

Forward an invocation of a variadic function in C

In C, is it possible to forward the invocation of a variadic function? As in, int my_printf(char *fmt, ...) { fprintf(stderr, "Calling printf with fmt %s", fmt); return SOMEHOW_INVOKE_LIBC_PRINTF; } Forwarding the invocation in the manner…
Patrick
  • 2,463
  • 2
  • 16
  • 6
232
votes
6 answers

How to make a variadic macro (variable number of arguments)

I want to write a macro in C that accepts any number of parameters, not a specific number example: #define macro( X ) something_complicated( whatever( X ) ) where X is any number of parameters I need this because whatever is overloaded and can be…
hasen
  • 161,647
  • 65
  • 194
  • 231
212
votes
5 answers

How to use R's ellipsis feature when writing your own function?

The R language has a nifty feature for defining functions that can take a variable number of arguments. For example, the function data.frame takes any number of arguments, and each argument becomes the data for a column in the resulting data table.…
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
92
votes
10 answers

Is it possible to iterate over arguments in variadic macros?

I was wondering if it is possible to iterate over arguments passed to a variadic macro in C99 or using any GCC extensions ? For e.g. is it possible to write a generic macro that takes a structure and its fields passed as arguments and prints offset…
vshenoy
  • 1,153
  • 1
  • 8
  • 14
59
votes
6 answers

C++11 variable number of arguments, same specific type

Question is simple, how would I implement a function taking a variable number of arguments (alike the variadic template), however where all arguments have the same type, say int. I was thinking about something alike this; void func(int...…
Skeen
  • 4,614
  • 5
  • 41
  • 67
54
votes
3 answers

How to create a variadic generic lambda?

Since C++14 we can use generic lambdas: auto generic_lambda = [] (auto param) {}; This basically means that its call operator is templated based on the parameters marked as auto. The question is how to create a lambda that can accept a variadic…
Drax
  • 12,682
  • 7
  • 45
  • 85
34
votes
4 answers

What is the (...) called in C and C++?

One of the uses of ... is to denote variadic entities in C and C++. What is its name? Is it classified as an operator or something else when used that way? Any other details regarding ...? Edit: I know the purpose of .... I am asking about its…
Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
33
votes
4 answers

mixing "exploded" slices and regular parameters in variadic functions

I'm wondering why it's not possible to do the following in go: func main() { stuff := []string{"baz", "bla"} foo("bar", stuff...) } func foo(s ...string) { fmt.Println(s) } In my understanding, slice... "explodes" the slice so it can…
Pascal
  • 5,879
  • 2
  • 22
  • 34
30
votes
2 answers

How do I handle an unspecified number of parameters in Scheme?

For example ((fn-stringappend string-append) "a" "b" "c") I know how to handle this (f x y z). But what if there's an unknown number of parameters? Is there any way to handle this kind of problem?
John
  • 827
  • 5
  • 15
  • 25
28
votes
8 answers

Creating a string list and an enum list from a C++ macro

In order to make my code shorter and easier to change I want to replace something like enum{ E_AAA, E_BBB, E_CCC }; static const char *strings{"AAA", "BBB", "CCC" }; With a macro, like INIT(AAA, BBB, CCC); but when I try doing a macro with variable…
Tiago
  • 271
  • 1
  • 3
  • 3
28
votes
2 answers

C++11 type trait to differentiate between enum class and regular enum

I'm writing a promotion template alias similar to boost::promote but for C++11. The purpose of this is to avoid warnings when retrieving arguments from varidic functions. e.g. template std::vector MakeArgVectorV(int aArgCount,…
Sam
  • 616
  • 7
  • 19
25
votes
3 answers

Does Haskell have variadic functions/tuples?

The uncurry function only works for functions taking two arguments: uncurry :: (a -> b -> c) -> (a, b) -> c If I want to uncurry functions with an arbitrary number of arguments, I could just write separate functions: uncurry2 f (a, b) = f…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
24
votes
2 answers

Count of parameters in a parameter pack? Is there a C++0x std lib function for this?

I was just wondering if there was anything in the C++0x std lib already available to count the number of parameters in a parameter pack? I'd like to get rid of the field_count in the code below. I know I can build my own counter, but it just seems…
Brett Rossier
  • 3,420
  • 3
  • 27
  • 36
23
votes
1 answer

GCC 4.8 is reversing variadic template parameter pack

I just upgraded to GCC 4.8 and some variadic template code no longer compiles correctly. I've created a minimal example below: #include #include template void something( std::tuple & tup…
Azoth
  • 1,652
  • 16
  • 24
1
2 3
44 45