variadic-functions

Varargs to ArrayList problem in Java

梦想与她 提交于 2019-12-03 18:46:08
问题 I don't understand why the following does not work: public void doSomething(int... args){ List<Integer> broken = new ArrayList<Integer>(Arrays.asList(args)) } Its my understanding that the compiler converts the "int... args" to an array, so the above code should work. Instead of working I get: cannot find symbol symbol: constructor ArrayList(java.util.List <int[] >) location: class java.util.ArrayList <java.lang.Integer > Thats bizarre. I'm not adding an array to array list, I'm adding each

Pass varargs to printf [duplicate]

左心房为你撑大大i 提交于 2019-12-03 16:48:32
问题 This question already has answers here : How to pass variable number of arguments to printf/sprintf (7 answers) Closed 5 years ago . I'd like to have a helper function log that essentially does the following: log(file, "array has %d elements\n", 10); // writes "2014-02-03 16:33:00 - array has 10 elements" to &file I have the time portion down, and I have the file writing portion down. However, the problem is the method signature itself for log — what should I put? This says that the printf

C/C++ va_list not returning arguments properly

拈花ヽ惹草 提交于 2019-12-03 16:41:49
I have a problem with using va_list. The below code works for an int: main() { int f1=1; float** m = function(n,f1); } float** function(int n,...) { va_list mem_list; va_start(mem_list, n); for (int i=0;i<n;i++) { for (int j=0;j<n;j++) { //m[i][j]=va_arg(mem_list,float); int f = va_arg(mem_list,int); printf("%i \n",f); } } va_end(mem_list); return NULL; } However when I change to a float i.e. float f1=1.0; float f = va_arg(mem_list,float); printf("%f \n",f); It does not return the right value (the value is 0.00000). I am extremely confused at what is happening. In the context of a varargs call

How to convert a List to variable argument parameter java

心已入冬 提交于 2019-12-03 14:17:50
问题 I have a method which takes a variable length string (String...) as parameter. I have a List<String> with me. How can I pass this to the method as argument? 回答1: String... equals a String[] So just convert your list to a String[] and you should be fine. 回答2: String ... and String[] are identical If you convert your list to array. using Foo[] array = list.toArray(new Foo[list.size()]); or Foo[] array = new Foo[list.size()]; list.toArray(array); then use that array as String ... argument to

Standard way to manipulate variadic arguments?

放肆的年华 提交于 2019-12-03 14:04:24
This is a weird question, but is there a standard way to manipulate the contents of a va_list before passing it to another function? For instance, suppose I have two functions, sum and vsum : int vsum(int n, va_list ap) { int total = 0; for (int i = 0; i < n; ++i) { total += va_arg(n, int); return total; } int sum(int n, ...) { va_list ap; va_start(ap, n); int total = vsum(n, ap); va_end(ap); return total; } If I call sum as sum(4, 1, 2, 3, 4) , I expect to get the result 10. Now let's suppose that instead of calling vsum directly, sum calls an intermediate function, vsum_stub which does the

SBRM/RAII for std::va_list/va_start()/va_end use

寵の児 提交于 2019-12-03 13:30:50
My code contains snippets like these: std::va_list ap; va_start(ap, msgfmt); snprintf_buf buf; const tchar * msg = buf.print_va_list(msgfmt, ap); va_end(ap); These are short and va_start() and va_end() are close together so they are not much of a problem. Exceptions from calls in between the two could be a problem (or not?). Simple test shows that calling va_start() from a function without ellipsis is not allowed. Is calling va_end() from a different function than va_start() was called from allowed or not? Basically, I am curious if it is possible to use the SBRM/RAII idiom for these calls,

Java 8 generic function should be ambiguous, but failing in runtime

久未见 提交于 2019-12-03 10:01:28
问题 I'm trying to migrate Java 7 code to Java 8, so I've code similar to: package tests; import java.util.Arrays; import java.util.Map; public class Tests { private static interface ComparableMap<K,V> extends Map<K,V>, Comparable {} public static void main(String[] args) { func(getString()); } private static void func(Comparable...input){ System.out.println(Arrays.toString(input)); } private static void func(ComparableMap <?,?> m){ System.out.println(m); } private static <T extends Comparable> T

What does the “…” mean in a parameter list? doInBackground(String… params)

拥有回忆 提交于 2019-12-03 08:43:50
问题 I don't understand that syntax. Trying to google various words plus "..." is useless. 回答1: This is called Variadic function (wiki page with examples in many languges). In computer programming, a variadic function is a function of indefinite arity, i.e. one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages. There are many mathematical and logical operations that come across naturally as variadic functions. For instance, the

What are the difference and use-cases for va_list, CVaListPointer, AnyObject …, CVarArgType?

只谈情不闲聊 提交于 2019-12-03 08:13:04
问题 Question Can someone please explain the differences between these argument types? Furthermore, if possible please supply appropriate use-cases using code (it's worth a 1000 words). Nota bene Please let me know in the comments if there is any requirement for more information. Background I am attempting to understand any differences between the following constructs and understand the appropriate use-cases (with examples if there are any). I've searched SO, Google, et, al. (blogosphere) without

Generate function of given arity in Haskell using type numbers

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 06:51:07
问题 Assume I have encoded the natural numbers in Haskell types, and that I have a way of adding and subtracting from them: data Zero data Succ n -- ... I have seen various bits of code which create the appearance of variadic functions, such as this, which allows the following: buildList "polyvariadic" "function" "wut?" :: [String] -- ["polyvariadic","function","wut?"] What I am wondering is whether I can build off of that to make a function which will only accept the number of arguments that