variadic-functions

Java variable number or arguments for a method

扶醉桌前 提交于 2019-11-26 05:18:14
问题 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 回答1: 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(

Specifying one type for all arguments passed to variadic function or variadic template function w/out using array, vector, structs, etc?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 04:48:06
问题 I\'m creating a function (possibly member function, not that it matters... maybe it does?) that needs to accept an unknown number of arguments, but I want all of them to be the same type. I know I could pass in an array or vector, but I want to be able to accept the list of args directly without extra structure or even extra brackets. It doesn\'t look like variadic functions by themselves are typesafe, and I wasn\'t sure how to go about this w/ variadic template functions. Here\'s essentially

Does printf(“%x”,1) invoke undefined behavior?

百般思念 提交于 2019-11-26 04:46:35
问题 According to the C standard (6.5.2.2 paragraph 6) If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions. If the number of arguments does not equal the number of parameters, the behavior is undefined. If the function is defined with a type that includes a prototype, and either the prototype

bug with varargs and overloading?

感情迁移 提交于 2019-11-26 03:46:48
问题 There seems to be a bug in the Java varargs implementation. Java can\'t distinguish the appropriate type when a method is overloaded with different types of vararg parameters. It gives me an error The method ... is ambiguous for the type ... Consider the following code: public class Test { public static void main(String[] args) throws Throwable { doit(new int[]{1, 2}); // <- no problem doit(new double[]{1.2, 2.2}); // <- no problem doit(1.2f, 2.2f); // <- no problem doit(1.2d, 2.2d); // <- no

Calling Java varargs method with single null argument?

我怕爱的太早我们不能终老 提交于 2019-11-26 03:39:38
问题 If I have a vararg Java method foo(Object ...arg) and I call foo(null, null) , I have both arg[0] and arg[1] as null s. But if I call foo(null) , arg itself is null. Why is this happening? How should I call foo such that foo.length == 1 && foo[0] == null is true ? 回答1: The issue is that when you use the literal null, Java doesn't know what type it is supposed to be. It could be a null Object, or it could be a null Object array. For a single argument it assumes the latter. You have two choices

How to properly match varargs in Mockito

巧了我就是萌 提交于 2019-11-26 03:28:31
问题 I\'ve been trying to get to mock a method with vararg parameters using Mockito: interface A { B b(int x, int y, C... c); } A a = mock(A.class); B b = mock(B.class); when(a.b(anyInt(), anyInt(), any(C[].class))).thenReturn(b); assertEquals(b, a.b(1, 2)); This doesn\'t work, however if I do this instead: when(a.b(anyInt(), anyInt())).thenReturn(b); assertEquals(b, a.b(1, 2)); This works, despite that I have completely omitted the varargs argument when stubbing the method. Any clues? 回答1:

How can I convert the “arguments” object to an array in JavaScript?

不问归期 提交于 2019-11-26 03:21:43
问题 The arguments object in JavaScript is an odd wart—it acts just like an array in most situations, but it\'s not actually an array object. Since it\'s really something else entirely, it doesn\'t have the useful functions from Array.prototype like forEach , sort , filter , and map . It\'s trivially easy to construct a new array from an arguments object with a simple for loop. For example, this function sorts its arguments: function sortArgs() { var args = []; for (var i = 0; i < arguments.length

An example of use of varargs in C

三世轮回 提交于 2019-11-26 02:57:04
问题 Here I found an example of how varargs can be used in C. #include <stdarg.h> double average(int count, ...) { va_list ap; int j; double tot = 0; va_start(ap, count); //Requires the last fixed parameter (to get the address) for(j=0; j<count; j++) tot+=va_arg(ap, double); //Requires the type to cast to. Increments ap to the next argument. va_end(ap); return tot/count; } I can understand this example only to some extent. It is not clear to me why we use va_start(ap, count); . As far as I

How to pass variable number of arguments to a PHP function

拈花ヽ惹草 提交于 2019-11-26 02:31:43
问题 I have a PHP function that takes a variable number of arguments (using func_num_args() and func_get_args() ), but the number of arguments I want to pass the function depends on the length of an array. Is there a way to call a PHP function with a variable number of arguments? 回答1: If you have your arguments in an array, you might be interested by the call_user_func_array function. If the number of arguments you want to pass depends on the length of an array, it probably means you can pack them

How to pass variable number of arguments to printf/sprintf

我是研究僧i 提交于 2019-11-26 02:29:21
问题 I have a class that holds an \"error\" function that will format some text. I want to accept a variable number of arguments and then format them using printf. Example: class MyClass { public: void Error(const char* format, ...); }; The Error method should take in the parameters, call printf/sprintf to format it and then do something with it. I don\'t want to write all the formatting myself so it makes sense to try and figure out how to use the existing formatting. 回答1: void Error(const char*