variadic-functions

How can a function with 'varargs' retrieve the contents of the stack?

守給你的承諾、 提交于 2019-11-28 17:18:45
Normally, in Delphi one would declare a function with a variable number of arguments using the 'array of const' method. However, for compatibility with code written in C, there's an much-unknown 'varargs' directive that can be added to a function declaration (I learned this while reading Rudy's excellent ' Pitfalls of convering ' document). As an example, one could have a function in C, declared like this : void printf(const char *fmt, ...) In Delphi, this would become : procedure printf(const fmt: PChar); varargs; My question is : How can I get to the contents of the stack when implementing a

Variable number of parameters in function in C++

青春壹個敷衍的年華 提交于 2019-11-28 17:01:56
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); } These are called Variadic functions . Wikipedia lists example code for C++ . To portably implement variadic functions in the C programming language, the standard stdarg.h header file should be used. The older varargs

NSString stringWithFormat swizzled to allow missing format numbered args

拈花ヽ惹草 提交于 2019-11-28 14:23:41
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 flags, major performance hits, or optimizations #define NUMARGS(...) (sizeof((int[]){__VA_ARGS__})

Variable argument constructor _may_ conflict, but compiles

老子叫甜甜 提交于 2019-11-28 13:45:37
I have two constructors that compile just fine but I'd expect Java to complain about the possibility of ambiguity. public Foo(int id, Bar bar, String name, String description){ } public Foo(int id, Bar bar, String... values){ } What gives? Java allows these methods to exist, because it has rules about which one will be called if both apply. Specifically, the fixed arity method (without ... ) will be chosen over the variable arity method (with ... ). The JLS, Section 15.12.2 , states the following when determining which method is chosen: The first phase (§15.12.2.2) performs overload resolution

Macro-producing macros in C?

人盡茶涼 提交于 2019-11-28 13:32:37
I'd like to get the C preprocessor to generate macros for me (i.e., I'm using C99 only). I'd write a macro #define make_macro(in) <...magic here...> and when I put make_macro(name1) make_macro(name2) later in the code, it would expand to #define name1(...) name1_fn(name1_info, __VA_ARGS__) #define name2(...) name2_fn(name2_info, __VA_ARGS__) and I'd then be able to use name1 and name2 as (macro-implemented) functions. I think I'm stuck using macros at both steps: it makes sense to use a macro to repeatedly re-fill a template, and the variadic argument handling won't work except via a macro. So

Most specific method with matches of both fixed/variable arity (varargs)

◇◆丶佛笑我妖孽 提交于 2019-11-28 12:41:09
In section 15.12.2.5 of the Java Language Specification , it talks about how to choose the most specific method in both cases of methods with fixed arity and methods of variable arity (i.e. varargs ). What I can't find in the JLS is anything about deciding between two methods where one is of fixed arity and one of variable arity however. For example: public interface SomeApi { public String getSomething(String arg); // method 1 public String getSomething(String ... args); // method 2 } Compiles just fine as one would expect ( for reasons outlined by Yoni below ). This calling code compiles

Why overload the varargs method of() in Java Stream interface?

烂漫一生 提交于 2019-11-28 12:24:17
The Stream interface has two overloads for the method of() . One of these is a variable-arity method while the other takes a single argument. Is the single-argument method a performance optimization versus passing one argument to the variable-arity method? If so, how does it improve performance? The same questions could be asked of the empty() method, which would seem to be syntax sugar around the variable-arity of() . I see that the implementation differs between these methods, with the difference apparently being how the Spliterator is instantiated; but what advantage does this offer to the

By-name repeated parameters

限于喜欢 提交于 2019-11-28 12:05:39
How to pass by-name repeated parameters in Scala? The following code fails to work: scala> def foo(s: (=> String)*) = { <console>:1: error: no by-name parameter type allowed here def foo(s: (=> String)*) = { ^ Is there any other way I could pass a variable number of by name parameters to the method? This isn't very pretty but it allows you to pass byname parameters varargs style def printAndReturn(s: String) = { println(s) s } def foo(s: (Unit => String)*) { println("\nIn foo") s foreach {_()} // Or whatever you want ... } foo() foo((Unit) => printAndReturn("f1"), (Unit) => printAndReturn("f2"

How to pass elements of an arrayList to variadic function

五迷三道 提交于 2019-11-28 11:55:05
I've got an arrayList filled with elements. I would like to pass the elements of that array list as arguments to a variadic function. My function public SequenceEntityModifier(final IEntityModifier... pEntityModifiers) My ArrayList ArrayList<IEntityModifier> arr = new ArrayList<IEntityModifier>(); arr.add(new MoveXModifier(1, 50, 120)); arr.add(new MoveXModifier(1, 120, 50)); I'd like to pass it to the function as if I would pass them individually. new SequenceEntityModifier( /* elements of arr here */ ); Is something like this possible? Thanks in advance. Brian Just do: new

Is a variable length argument treated as an array in Java?

一笑奈何 提交于 2019-11-28 10:51:57
As I understand an array consists of fixed number of elements and a variable length argument takes as many number of arguments as you pass (of the same type). But are they same? Can I pass one where the other is expected? Yes, if you have a method with a varargs parameter like this: public void foo(String... names) and you call it like this: foo("x", "y", "z"); then the compiler just converts that into: foo(new String[] { "x", "y", "z"}); The type of the names parameter is String[] , and can be used just like any other array variable. Note that it could still be null : String[] nullNames =