variadic-functions

How can you pass multiple primitive parameters to AsyncTask?

我是研究僧i 提交于 2019-11-26 19:23:52
There are related questions, such as How can I pass in 2 parameters to a AsyncTask class? , but I ran into the difficulty of trying in vain to pass multiple primitives as parameters to an AsyncTask, so I want to share what I discovered. This subtlety is not captured in the existing questions and answers, so I want to help out anyone who runs into the same problem as I did and save them the pain. The question is this: I have multiple primitive parameters (e.g. two longs) that I want to pass to an AsyncTask to be executed in the background--how can it be done? (My answer...after struggling with

Technically, how do variadic functions work? How does printf work?

旧城冷巷雨未停 提交于 2019-11-26 19:01:36
问题 I know I can use va_arg to write my own variadic functions, but how do variadic functions work under the hood, i.e. on the assembly instruction level? E.g., how is it possible that printf takes a variable number of arguments? * No rule without exception. There is no language C/C++, however, this question can be answered for both of them * Note: Answer originally given to How can printf function can take variable parameters in number while output them?, but it seems it did not apply to the

How to work with varargs and reflection

半世苍凉 提交于 2019-11-26 18:55:43
Simple question, how make this code working ? public class T { public static void main(String[] args) throws Exception { new T().m(); } public // as mentioned by Bozho void foo(String... s) { System.err.println(s[0]); } void m() throws Exception { String[] a = new String[]{"hello", "kitty"}; System.err.println(a.getClass()); Method m = getClass().getMethod("foo", a.getClass()); m.invoke(this, (Object[]) a); } } Output: class [Ljava.lang.String; Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

What is the format of the x86_64 va_list structure?

可紊 提交于 2019-11-26 18:50:48
Anyone have a reference for the representation of va_list in the x86_64 ABI (the one used on Linux)? I'm trying to debug some code where the stack or arguments seem corrupt and it would really help to understand what I'm supposed to be seeing... Skurmedel I made my comment into an answer. This may help . It's a reference, albeit lightweight ( EDIT : original link dead; replaced Wayback Machine-preserved link). The Variable Argument List reference starts on page 50, then it goes on, page 52-53 documents va_list : The va_list Type The va_list type is an array containing a single element of one

Java SafeVarargs annotation, does a standard or best practice exist?

假装没事ソ 提交于 2019-11-26 18:45:19
问题 I've recently come across the java @SafeVarargs annotation. Googling for what makes a variadic function in Java unsafe left me rather confused (heap poisoning? erased types?), so I'd like to know a few things: What makes a variadic Java function unsafe in the @SafeVarargs sense (preferably explained in the form of an in-depth example)? Why is this annotation left to the discretion of the programmer? Isn't this something the compiler should be able to check? Is there some standard one must

Java variable number or arguments for a method

泄露秘密 提交于 2019-11-26 18:25:13
Is it possible to declare a method that will allow a variable number of parameters ? What is the symbolism used in the definition that indicate that the method should allow a variable number of parameters? Answer: varargs BalusC That's correct. You can find more about it in the Oracle guide on varargs . Here's an example: void foo(String... args) { for (String arg : args) { System.out.println(arg); } } which can be called as foo("foo"); // Single arg. foo("foo", "bar"); // Multiple args. foo("foo", "bar", "lol"); // Don't matter how many! foo(new String[] { "foo", "bar" }); // Arrays are also

How does Haskell printf work?

倖福魔咒の 提交于 2019-11-26 17:56:03
问题 Haskell's type safety is second to none only to dependently-typed languages. But there is some deep magic going on with Text.Printf that seems rather type-wonky. > printf "%d\n" 3 3 > printf "%s %f %d" "foo" 3.3 3 foo 3.3 3 What is the deep magic behind this? How can the Text.Printf.printf function take variadic arguments like this? What is the general technique used to allow for variadic arguments in Haskell, and how does it work? (Side note: some type safety is apparently lost when using

Concatenate two slices in Go

天涯浪子 提交于 2019-11-26 17:53:12
问题 I'm trying to combine the slice [1, 2] and the slice [3, 4] . How can I do this in Go? I tried: append([]int{1,2}, []int{3,4}) but got: cannot use []int literal (type []int) as type int in append However, the documentation seems to indicate this is possible, what am I missing? slice = append(slice, anotherSlice...) 回答1: Add dots after the second slice: //---------------------------vvv append([]int{1,2}, []int{3,4}...) This is just like any other variadic function. func foo(is ...int) { for i

Why does printf() promote a float to a double?

守給你的承諾、 提交于 2019-11-26 17:50:28
From a previous question : If you attempt to pass a float to printf , it'll be promoted to double before printf receives it printf() is a variadic function right? So does a variadic function promote a float argument to a double before passing it? Yes, float arguments to variadic function are promoted to double. The draft C99 standard section 6.5.2.2 Function calls says: [...]and arguments that have type float are promoted to double. These are called the default argument promotions.[...] from the draft C++ standard section 5.2.2 Function call: [...]a floating point type that is subject to the

Why does type-promotion take precedence over varargs for overloaded methods

耗尽温柔 提交于 2019-11-26 17:48:55
问题 public class Test { public static void printValue(int i, int j, int k) { System.out.println("int"); } public static void printValue(byte...b) { System.out.println("long"); } public static void main(String... args) { byte b = 9; printValue(b,b,b); } } The output of the above code is "int". But it should be "long" because byte type argument function is already present. But here the program is promoting the byte values to int, but it should not be the case. Please can someone clarify what is