variadic-functions

Variable number of parameters in function in C++

。_饼干妹妹 提交于 2019-11-27 10:09:36
问题 How I can have variable number of parameters in my function in C++. Analog in C#: public void Foo(params int[] a) { for (int i = 0; i < a.Length; i++) Console.WriteLine(a[i]); } public void UseFoo() { Foo(); Foo(1); Foo(1, 2); } Analog in Java: public void Foo(int... a) { for (int i = 0; i < a.length; i++) System.out.println(a[i]); } public void UseFoo() { Foo(); Foo(1); Foo(2); } 回答1: These are called Variadic functions. Wikipedia lists example code for C++. To portably implement variadic

Concatenate two slices in Go

随声附和 提交于 2019-11-27 09:58:53
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...) 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 := 0; i < len(is); i++ { fmt.Println(is[i]) } } func main() { foo([]int{9,8,7,6,5}...) } Appending to and

Error with varargs for function-objects in Scala?

空扰寡人 提交于 2019-11-27 09:12:33
Why does this not work? val f = (args: Int*) => args.sum error: ')' expected but identifier found. val f = (args: Int*) => args.sum ^ This however works perfectly fine def sum(args: Int*) = args.sum val f = sum _ so does this val f: (Int*) => Int = args => args.sum btw. I'm using scala 2.9.1 I'm not an expert in specification reading, but it looks like the varargs Syntax is not supported for anonymous function. Compare the syntax for Function Declaration vs Anonymous Functions in the Language Spec From 4.6 Function Declarations and Definitions ParamType ::= Type | ‘=>’ Type | Type ‘*’ 6.23

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

送分小仙女□ 提交于 2019-11-27 09:08:16
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 going on here? Variable argument methods will always be the last one to be chosen by the compiler in case

Populating a va_list

微笑、不失礼 提交于 2019-11-27 09:01:28
Is there a way to create a va_list from scratch? I'm trying to call a function that takes a va_list as a parameter: func(void **entry, int num_args, va_list args, char *key); ...from a function that doesn't take a variable number of arguments. The only way I can think of is to create an intermediary function that takes varargs and then passing along its va_list, which is pretty stupid: void stupid_func(void **entry, char *key, int num_args, ...) { va_list args; va_start(args, num_args); func(entry, num_args, args, key); va_end(args); } Is there a better way? I can't change func 's signature.

Is GCC mishandling a pointer to a va_list passed to a function?

我怕爱的太早我们不能终老 提交于 2019-11-27 08:51:05
The question 'Pass va_list or pointer to va_list?' has an answer which quotes the standard (ISO/IEC 9899:1999 - §7.15 'Variable arguments <stdarg.h> , footnote 212) as explicitly saying that: It is permitted to create a pointer to a va_list and pass that pointer to another function, in which case the original function may make further use of the original list after the other function returns. I'm compiling some code which can be exemplified by the following (the real code is very considerably more complex, with the original functions doing a lot more work than shown here). vap.c #include

Why doesn't autoboxing overrule varargs when using method overloading in Java 7?

我怕爱的太早我们不能终老 提交于 2019-11-27 08:38:37
We have a class LogManager in our Java project which looks like this: public class LogManager { public void log(Level logLevel, Object... args) { // do something } public void log(Level logLevel, int value, Object... args) { // do something else } } When compiling the project with OpenJDK 6 under Debian everyting works fine. When using OpenJDK 7 the build (done with ant) produces the following errors and the build fails: [javac] /…/LogManager.java:123: error: reference to log is ambiguous, both method log(Level,Object...) in LogManager and method log(Level,int,Object...) in LogManager match

NSString stringWithFormat swizzled to allow missing format numbered args

时间秒杀一切 提交于 2019-11-27 08:37:38
问题 Based on this SO question asked a few hours ago, I have decided to implement a swizzled method that will allow me to take a formatted NSString as the format arg into stringWithFormat , and have it not break when omitting one of the numbered arg references ( %1$@, %2$@ ) I have it working, but this is the first copy, and seeing as this method is going to be potentially called hundreds of thousands of times per app run, I need to bounce this off of some experts to see if this method has any red

What are the automatic type promotions of variadic function arguments?

不羁的心 提交于 2019-11-27 07:55:40
问题 Consider the following code snippet: #include <stdio.h> #include <stdarg.h> void display(int num, ...) { char c; int j; va_list ptr; va_start(ptr,num); for (j= 1; j <= num; j++){ c = va_arg(ptr, char); printf("%c", c); } va_end(ptr); } int main() { display(4, 'A', 'a', 'b', 'c'); return 0; } The program gives runtime error because vararg automatically promotes char to int, and i should have used int in this case. What are all types are permitted when I use vararg, how to know which type to

Using varargs from Scala

爷,独闯天下 提交于 2019-11-27 07:03:05
I'm tearing my hair out trying to figure out how to do the following: def foo(msf: String, o: Any, os: Any*) = { println( String.format(msf, o :: List(os:_*)) ) } There's a reason why I have to declare the method with an o and an os Seq separately. Basically, I end up with the format method called with a single object parameter (of type List ). Attempting: def foo(msf: String, o: Any, os: Any*) = { println( String.format(msf, (o :: List(os:_*))).toArray ) } Gives me the type error: found: Array[Any] required Seq[java.lang.Object] I've tried casting, which compiles but fails for pretty much the