variadic-functions

Call Method.invoke() when arguments in array

跟風遠走 提交于 2019-12-01 23:48:25
问题 I have the following interface: interface Foo { void bar(String a, int b); } I want to invoke Foo.bar (on an implementation of Foo) reflectively. However, the arguments are in array and i do not know the size of it. The following does not work: void gee(Foo someFoo, Method bar, Object[] args) { bar.invoke(someFoo, args); } That does not work because args is threated by the compiler as a single argument and the array is not "expanded" to vararg but is wrapped (internally) in one more array

How to determine the end of va_arg list?

旧时模样 提交于 2019-12-01 23:30:29
I have a function foo(char *n, ...); I need to get and use all of optional char parameters. I had an idea to use while(va_arg(argPtr, char) != NULL) { ... } to understand when I reach the end of the list. So, will it work, if in function call I'll do foo(n, 't', 'm', '$', NULL); ? Will NULL be read as a char by va_arg? Or maybe there's a more common way to determine the end of list, without adding NULL as last parameter? There is no direct way for a function that uses va_arg to determine the number or type(s) of the arguments passed by a given call. Your proposed method in particular: while(va

How to determine the end of va_arg list?

风格不统一 提交于 2019-12-01 23:06:52
问题 I have a function foo(char *n, ...); I need to get and use all of optional char parameters. I had an idea to use while(va_arg(argPtr, char) != NULL) { ... } to understand when I reach the end of the list. So, will it work, if in function call I'll do foo(n, 't', 'm', '$', NULL); ? Will NULL be read as a char by va_arg? Or maybe there's a more common way to determine the end of list, without adding NULL as last parameter? 回答1: There is no direct way for a function that uses va_arg to determine

Am I missing something, or do varargs break Arrays.asList?

[亡魂溺海] 提交于 2019-12-01 21:44:31
private void activateRecords(long[] stuff) { ... api.activateRecords(Arrays.asList(specIdsToActivate)); } Shouldn't this call to Arrays.asList return a list of Long s? Instead it is returning a List<long[]> public static <T> List<T> asList(T... a) The method signature is consistent with the results, the varargs throws the entire array into the list. It's the same as new ArrayList(); list.add(myArray) And yes, I know it's meant to be used like this: Arrays.asList(T t1, T t2, T t3) I guess what I'm getting at, is instead of the varargs form, why can't I just have my old asList method (at least I

Call Method.invoke() when arguments in array

折月煮酒 提交于 2019-12-01 21:04:35
I have the following interface: interface Foo { void bar(String a, int b); } I want to invoke Foo.bar (on an implementation of Foo) reflectively. However, the arguments are in array and i do not know the size of it. The following does not work: void gee(Foo someFoo, Method bar, Object[] args) { bar.invoke(someFoo, args); } That does not work because args is threated by the compiler as a single argument and the array is not "expanded" to vararg but is wrapped (internally) in one more array with single element, i.e. @Test public void varArgTest() { assertTrue(varArgFoo(new Object[] {1, 2}) == 1)

Undefined number of arguments in C++ [duplicate]

无人久伴 提交于 2019-12-01 20:41:30
This question already has an answer here: Variable number of arguments in C++? 16 answers Can I overload my function to do something with a lot of arguments as in JavaScript . For example: function f() { alert(arguments[0]); } f(4); // will alert 4 Can I do the same thing in C++ ? You can use variadic template arguments and tuples: #include <tuple> #include <iostream> template <class... Args> void f(Args&&... args) { auto arguments = std::make_tuple(std::forward<Args>(args)...); std::cout << std::get<0>(arguments); } void f() {} // for 0 arguments int main() { f(2, 4, 6, 8); } Live Demo For

How do I handle a variable number of arguments passed to a function in Racket?

♀尐吖头ヾ 提交于 2019-12-01 20:16:36
问题 I like creating functions which take an unlimited number of arguments, and being able to deal with them as a list. It's been useful to me when creating binary trees & I'm using it for a variation on the nearest-neighbour algorithm right now. My method, however, is really horrible: since I can't think of a way to iterate over an improper list (which may well be improper & degenerate), I tried using various list functions to force the improper list into list form. This is my best attempt in a

How do I handle a variable number of arguments passed to a function in Racket?

谁说胖子不能爱 提交于 2019-12-01 20:09:55
I like creating functions which take an unlimited number of arguments, and being able to deal with them as a list. It's been useful to me when creating binary trees & I'm using it for a variation on the nearest-neighbour algorithm right now. My method, however, is really horrible: since I can't think of a way to iterate over an improper list (which may well be improper & degenerate), I tried using various list functions to force the improper list into list form. This is my best attempt in a simple function to determine difference between map-nodes (works, just not sure why it works): (define

ClassCastException while using varargs and generics

我与影子孤独终老i 提交于 2019-12-01 19:20:33
I'm using java generics and varargs. If I use the following code, I'll get a ClassCastException , even though I'm not using casts at all. Stranger yet, if I run this on Android (dalvik) no stack trace is included with the exception, and if I change the interface to abstract class, the exception variable e is empty. The code: public class GenericsTest { public class Task<T> { public void doStuff(T param, Callback<T> callback) { // This gets called, param is String "importantStuff" // Working workaround: //T[] arr = (T[]) Array.newInstance(param.getClass(), 1); //arr[0] = param; //callback

Undefined number of arguments in C++ [duplicate]

此生再无相见时 提交于 2019-12-01 19:16:43
问题 This question already has answers here : Variable number of arguments in C++? (16 answers) Closed 6 years ago . Can I overload my function to do something with a lot of arguments as in JavaScript . For example: function f() { alert(arguments[0]); } f(4); // will alert 4 Can I do the same thing in C++ ? 回答1: You can use variadic template arguments and tuples: #include <tuple> #include <iostream> template <class... Args> void f(Args&&... args) { auto arguments = std::make_tuple(std::forward