variadic-functions

Why use variadic arguments now when initializer lists are available?

限于喜欢 提交于 2019-11-27 00:22:14
问题 I've been wondering what are the advantages of variadic arguments over initializer lists. Both offer the same ability - to pass indefinite number of arguments to a function. What I personally think is initializer lists are a little more elegant. Syntax is less awkward. Also, it appears that initializer lists have significantly better performance as the number of arguments grows. So what am I missing, besides the possibility to use use variadic arguments in C as well? 回答1: If by variadic

passing variable number of arguments

跟風遠走 提交于 2019-11-26 23:36:33
问题 Can we pass variable number of arguments to a function in c? 回答1: Here is an example: #include <stdlib.h> #include <stdarg.h> #include <stdio.h> int maxof(int, ...) ; void f(void); int main(void){ f(); exit(EXIT SUCCESS); } int maxof(int n_args, ...){ register int i; int max, a; va_list ap; va_start(ap, n_args); max = va_arg(ap, int); for(i = 2; i <= n_args; i++) { if((a = va_arg(ap, int)) > max) max = a; } va_end(ap); return max; } void f(void) { int i = 5; int j[256]; j[42] = 24; printf("%d

Java generics and varargs

前提是你 提交于 2019-11-26 22:43:22
问题 I'd like to implement a function with both generics and varargs. public class Question { public static <A> void doNastyThingsToClasses(Class<A> parent, Class<? extends A>... classes) { /*** something here ***/ } public static class NotQuestion { } public static class SomeQuestion extends Question { } public static void main(String[] args) { doNastyThingsToClasses(Object.class, Question.class, SomeQuestion.class); // OK doNastyThingsToClasses(Question.class, SomeQuestion.class); // OK

Passing lists from one function to another in Swift

醉酒当歌 提交于 2019-11-26 22:34:08
I can't quite understand why I can't past an Int[] from one function to another: func sumOf(numbers: Int...) -> Int { var sum = 0 for number in numbers { sum += number } return sum } func average(numbers:Int...) -> Double { var sum = sumOf(numbers) return Double(sum) / Double(numbers.count) } This gives me the following error: Playground execution failed: error: <REPL>:138:19: error: could not find an overload for '__conversion' that accepts the supplied arguments var sum = sumOf(numbers) ^~~~~~~~~~~~~~ Thanks for your help! The numbers: Int... parameter in sumOf is called a variadic parameter

What is the point of overloaded Convenience Factory Methods for Collections in Java 9

纵饮孤独 提交于 2019-11-26 22:24:07
Java 9 comes with convenience factory methods for creating immutable lists. Finally a list creation is as simple as: List<String> list = List.of("foo", "bar"); But there are 12 overloaded versions of this method, 11 with 0 to 10 elements, and one with var args. static <E> List<E> of(E... elements) Same is the case with Set and Map . Since there is a var args method, what is the point of having extra 11 methods? What I think is that var-args create an array, so the other 11 methods can skip creation of an extra object and in most cases 0 - 10 elements will do. Is there any other reason for this

Python, default keyword arguments after variable length positional arguments

北城余情 提交于 2019-11-26 22:17:29
I thought I could use named parameters after variable-length positional parameters in a function call in Python 2, but I get a SyntaxError when importing a python class. I'm writing with the following "get" method, for example: class Foo(object): def __init__(self): print "You have created a Foo." def get(self, *args, raw=False, vars=None): print len(args) print raw print vars The error looks like: def get(self, *args, raw=False, vars=None): ^ SyntaxError: invalid syntax I'd like to be able to call the method several ways: f = Foo() f.get(arg1, arg2) f.get(arg1, raw=True) f.get(arg1, arg2, raw

How to create a function and pass in variable length argument list?

旧时模样 提交于 2019-11-26 22:09:19
问题 We can create a function p in the following code: var p = function() { }; if (typeof(console) != 'undefined' && console.log) { p = function() { console.log(arguments); }; } but the arguments are passed like an array to console.log , instead of passed one by one as in console.log(arguments[0], arguments[1], arguments[2], ... Is there a way to expand the arguments and pass to console.log like the way above? Note that if the original code were var p = function() { }; if (typeof(console) !=

Macro-producing macros in C?

非 Y 不嫁゛ 提交于 2019-11-26 22:05:34
问题 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

How to create variable argument methods in Objective-C

扶醉桌前 提交于 2019-11-26 21:36:43
Maybe this will be obviously simple for most of you, but could you please give an example how to create similar methods (in Objective-C) and functions in C to create functions like NSString 's stringWithFormat: , or NSLog() . Just to remind: [NSString stringWithFormat:@"example tekst %i %@ %.2f", 122, @"sth", 3.1415"]; NSLog(@"account ID %i email %@", accountID, email); I'd like to create the similar to NSString 's method stringWithFormat: , NSURL - urlWithFormat . Williham Totland What these are called, generally, is "variadic functions" (or methods, as it were). To create this, simply end

difference fn(String… args) vs fn(String[] args)

笑着哭i 提交于 2019-11-26 21:35:33
Whats this syntax useful for : function(String... args) Is this same as writing function(String[] args) with difference only while invoking this method or is there any other feature involved with it ? The only difference between the two is the way you call the function. With String var args you can omit the array creation. public static void main(String[] args) { callMe1(new String[] {"a", "b", "c"}); callMe2("a", "b", "c"); // You can also do this // callMe2(new String[] {"a", "b", "c"}); } public static void callMe1(String[] args) { System.out.println(args.getClass() == String[].class); for