variadic-functions

How do I fill a va_list

巧了我就是萌 提交于 2019-11-28 09:42:48
If I have a va_list I know how to extract all its elements: void printInts(int n,...) { va_list va; va_start(va, n); for(unsigned int i=0; i<n; i++) { int arg=va_arg(va, int); printf("%d",arg); } va_end(va); } So when I call printInts(3,1,2,3) the va_list get filled of all the parameters. But how do I manually fill a va_list without using va_start? I mean that I want something like: va_list va; push_arg(va, int, 5); // And so on until I fill all parameters ... I need this because there is a function that accept a va_list as argument, and I don't know how to fill that va_list of all its

How to use a variadic closure in swift?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 09:18:37
问题 Is there a proper way to use a variadic parameter list in a closure in Swift? In swift I notice that I can declare a function which takes a variadic argument list like so protocol NumberType: Comparable, IntegerLiteralConvertible, IntegerArithmeticType {} extension Int: NumberType {} extension SequenceType where Generator.Element: NumberType { func satisfy(closure:(args: Generator.Element...) -> ()) { // Do something closure(args: 1, 2, 3) } } Which builds just fine. When I try to use the

Inserting a variadic argument list into a vector?

a 夏天 提交于 2019-11-28 08:07:48
Forgive me if this has been answered already, as I couldn't find it... Basically I have an object that needs to take a variadic argument list in it's constructor and store the arguments in a vector. How do I initialize a vector from a the arguments of a variadic constructor? class GenericNode { public: GenericNode(GenericNode*... inputs) { /* Something like... */ // inputs_.push_back(inputs)...; } private: std::vector<GenericNode*> inputs_; }; The best thing would be to use an initializer list #include <initializer_list> #include <vector> class GenericNode { public: GenericNode(std:

Generic functor for functions with any argument list

空扰寡人 提交于 2019-11-28 07:53:05
I need to implement a functor that takes any (!) function pointer when instantiated, analyses the argument types, stores the pointer and when operator() is called, does something with the pointer. The easiest case being, calling the function with its arguments. I tried converting the function pointer to something like std::function and I get the error: error: invalid use of incomplete type ‘struct std::function<void (*)(...)>’ /usr/include/c++/4.6/functional:1572:11: error: declaration of ‘struct std::function<void(*)(...)>’ I am using gcc 4.6.1, compiling with -std=c++0x This is a minimal

Why do Guava classes provide so many factory methods instead of just one that takes varargs? [duplicate]

Deadly 提交于 2019-11-28 07:13:48
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Why does Guava's ImmutableList have so many overloaded of() methods? Looking at Guava's ImmutableList (and some other classes), you'll find plenty of overloaded of convenience methods ("Returns an immutable list containing the given elements, in order.") that take a different number of parameters: ... public static <E> ImmutableList<E> of(E e1, E e2, E e3) public static <E> ImmutableList<E> of(E e1, E e2, E e3,

Java unchecked: unchecked generic array creation for varargs parameter

旧城冷巷雨未停 提交于 2019-11-28 05:35:50
I have set Netbeans to show unchecked warnings in my Java code, but I am failing to understand the error on the following lines: private List<String> cocNumbers; private List<String> vatNumbers; private List<String> ibans; private List<String> banks; ... List<List<String>> combinations = Utils.createCombinations(cocNumbers, vatNumbers, ibans); Gives: [unchecked] unchecked generic array creation for varargs parameter of type List<String>[] Method source: /** * Returns a list of all possible combinations of the entered array of lists. * * Example: [["A", "B"], ["0", "1", "2"]] * Returns: [["A",

What exactly is va_end for? Is it always necessary to call it?

时间秒杀一切 提交于 2019-11-28 04:49:42
va_end - Macro to reset arg_ptr . After accessing a variable argument list, the arg_ptr pointer is usually reset with va_end() . I understand that it is required if you want to re-iterate the list, but is it really needed if you aren't going to? Is it just good practice, like the rule "always have a default: in your switch "? greyfade va_end is used to do cleanup. You don't want to smash the stack, do you? From man va_start : va_end() Each invocation of va_start() must be matched by a corresponding invocation of va_end() in the same function. After the call va_end(ap) the variable ap is

Why use variadic arguments now when initializer lists are available?

做~自己de王妃 提交于 2019-11-28 04:22:30
I've been wondering what are the advantages of variadic arguments over initializer lists. Both offer the same ability - to pass indefinite number of arguments to a function. What I personally think is initializer lists are a little more elegant. Syntax is less awkward. Also, it appears that initializer lists have significantly better performance as the number of arguments grows. So what am I missing, besides the possibility to use use variadic arguments in C as well? Andy Prowl If by variadic arguments you mean the ellipses (as in void foo(...) ), then those are made more or less obsolete by

Why do argument lists in certain Cocoa methods end with a nil?

好久不见. 提交于 2019-11-28 01:57:15
Why do argument list in some methods end with nil ? I have noticed this particularly in the collection classes, for example NSSet : mySet = [NSSet setWithObjects:someData, aValue, aString, nil]; and NSArray : NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", @"value3", nil]; It has to do with how variable argument lists work ( va_list , seen as ... in the parameters). When the code is trying to extract all of the values in the list, it needs to know when to stop (because it doesn't know how many there are). We denote the end of the list with a special value called a "sentinel"

passing variable number of arguments

送分小仙女□ 提交于 2019-11-28 01:52:49
Can we pass variable number of arguments to a function in c? Preet Sangha Here is an example: #include <stdlib.h> #include <stdarg.h> #include <stdio.h> int maxof(int, ...) ; void f(void); int main(void){ f(); exit(EXIT SUCCESS); } int maxof(int n_args, ...){ register int i; int max, a; va_list ap; va_start(ap, n_args); max = va_arg(ap, int); for(i = 2; i <= n_args; i++) { if((a = va_arg(ap, int)) > max) max = a; } va_end(ap); return max; } void f(void) { int i = 5; int j[256]; j[42] = 24; printf("%d\n", maxof(3, i, j[42], 0)); } If it is a function that accepts a variable number of arguments