variadic-functions

Why isn't Math.max(double a, double b) variadic?

烂漫一生 提交于 2020-04-06 05:22:54
问题 Why isn't the implementation of Math.max a variadic function? It could get implemented like this: public class Main { public static double max(double... values) { double max = Double.NEGATIVE_INFINITY; for (double tmp : values) { max = max < tmp ? tmp : max; } return max; } public static void main(String[] args) { // This works fine: System.out.println(max(-13, 12, 1337, 9)); // This doesn't work: // System.out.println(Math.max(-13, 12, 1337)); } } Is there any reason why it is not

_Generic combined with variadic function?

拜拜、爱过 提交于 2020-03-19 07:49:11
问题 In C11, I could create a function which prototype would look like this: void myVaFunc(const char* const conv, ...); I could run it like this: myVaFunc("ici", 1, "test", 2); The function would know (after parsing the 1st parameter) that there are 3 additional parameters (4 with the initial one) with types consequently int , string (char pointer) and int . Easy, but not very elegant. Recently I have learned about the _Generic keyword, which allows to derive the type of a variable at the

_Generic combined with variadic function?

泪湿孤枕 提交于 2020-03-19 07:48:25
问题 In C11, I could create a function which prototype would look like this: void myVaFunc(const char* const conv, ...); I could run it like this: myVaFunc("ici", 1, "test", 2); The function would know (after parsing the 1st parameter) that there are 3 additional parameters (4 with the initial one) with types consequently int , string (char pointer) and int . Easy, but not very elegant. Recently I have learned about the _Generic keyword, which allows to derive the type of a variable at the

_Generic combined with variadic function?

こ雲淡風輕ζ 提交于 2020-03-19 07:47:32
问题 In C11, I could create a function which prototype would look like this: void myVaFunc(const char* const conv, ...); I could run it like this: myVaFunc("ici", 1, "test", 2); The function would know (after parsing the 1st parameter) that there are 3 additional parameters (4 with the initial one) with types consequently int , string (char pointer) and int . Easy, but not very elegant. Recently I have learned about the _Generic keyword, which allows to derive the type of a variable at the

C variadic function: How to specify which type to give to va_arg

人走茶凉 提交于 2020-03-18 06:53:19
问题 In a function like printf, we use stdarg.h to handle the variadic parameters. void print(int args,...){ va_list ap; va_start(ap, args); int i = 0; for(i=0; i<args; i++){ printf("%d\n",va_arg(ap, int)); } va_end(ap); } We want to parse the format list (the first argument given to our variadic function) to track the types of the arguments specified in the format list then, call va_arg with the appropriate type. I make a first loop to parse the format list, store the specifiers letters into an

Could adding variadic parameters to a function break existing code?

[亡魂溺海] 提交于 2020-02-23 09:36:41
问题 Is adding a variadic parameter to an existing Go function a breaking change? For example: // Old function func Foo(a int) // Updated to: func Foo(a int, params ...string) Callers of the API can omit the new parameter, so I would think the API is backwards-compatible. Can anyone provide an example where a user of the old API could not use the new API without changing their code? 回答1: I. Changing functions Calling them will continue to work without modification, but since the function

Could adding variadic parameters to a function break existing code?

眉间皱痕 提交于 2020-02-23 09:36:05
问题 Is adding a variadic parameter to an existing Go function a breaking change? For example: // Old function func Foo(a int) // Updated to: func Foo(a int, params ...string) Callers of the API can omit the new parameter, so I would think the API is backwards-compatible. Can anyone provide an example where a user of the old API could not use the new API without changing their code? 回答1: I. Changing functions Calling them will continue to work without modification, but since the function

Multiple object types for varargs in a method prototype?

三世轮回 提交于 2020-02-23 08:58:28
问题 I'm trying to write the prototype of a Java function that can be called with any number of integers and strings: myMethod(1, 2, 3, "Hello", "World"); // Valid call myMethod(4, "foo", "bar", "foobar"); // Valid call Ideally, I would like the ints and strings to be given in any order (and possibly mixed): myMethod(1, "Hello", 2, "World", 3); // Valid call I thought of using varargs, but there can be only one in the prototype. Another idea I've had is to use the following prototype: public void

Varargs methods and primitive types [duplicate]

不羁的心 提交于 2020-02-06 06:05:56
问题 This question already has answers here : Arrays.asList() not working as it should? (10 answers) Closed 2 years ago . In Effective Java J. Bloch mentioned that it was not safe to use varargs method with primitive types. Percisely, Arrays.asList(1, 2, 4) had return type List<int[]> and it sounds quite reasonble. Now I tried to reproduce this behaviour myself and couldn't: My question is why is the type deduced to List<Integer> but not to List<int[]> as he stated? Does it mean, that in Java 8

windll ctypes call variadic c function from python 2.7 works in win64 but not in win32

血红的双手。 提交于 2020-01-30 08:19:29
问题 I'm using Python 2.7 on Windows 10-32 and Windows 10-64. I'm writing a python wrapper to a C compiled stdcall (Windows) DLL (= mydll). I have 2 versions of the DLL - 32 and 64 bit. The 64 version works great using windll.mydll . The 32 version works great using the same command for all functions on the DLL, except for variadic printf -like functions. When running mydll.myvarfunc("Hello") I get ValueError: Procedure probably called with too many arguments (4 bytes in excess) Is there a way