variadic-functions

Possible heap pollution via varargs parameter

廉价感情. 提交于 2019-11-26 02:28:38
问题 I understand this occurs with Java 7 when using varargs with a generic type; But my question is.. What exactly does Eclipse mean when it says \"its use could potentially pollute the heap?\" And How does the new @SafeVarargs annotation prevent this? 回答1: Heap pollution is a technical term. It refers to references which have a type that is not a supertype of the object they point to. List<A> listOfAs = new ArrayList<>(); List<B> listOfBs = (List<B>)(Object)listOfAs; // points to a list of As

trailing return type using decltype with a variadic template function

纵饮孤独 提交于 2019-11-26 02:23:43
问题 I want to write a simple adder (for giggles) that adds up every argument and returns a sum with appropriate type. Currently, I\'ve got this: #include <iostream> using namespace std; template <class T> T sum(const T& in) { return in; } template <class T, class... P> auto sum(const T& t, const P&... p) -> decltype(t + sum(p...)) { return t + sum(p...); } int main() { cout << sum(5, 10.0, 22.2) << endl; } On GCC 4.5.1 this seems to work just fine for 2 arguments e.g. sum(2, 5.5) returns with 7.5

Passing variable arguments to another function that accepts a variable argument list

一世执手 提交于 2019-11-26 01:59:08
问题 So I have 2 functions that both have similar arguments void example(int a, int b, ...); void exampleB(int b, ...); Now example calls exampleB , but how can I pass along the variables in the variable argument list without modifying exampleB (as this is already used elsewhere too). 回答1: You can't do it directly; you have to create a function that takes a va_list : #include <stdarg.h> static void exampleV(int b, va_list args); void exampleA(int a, int b, ...) // Renamed for consistency { va_list

How do I define Lisp’s apply in Haskell?

拥有回忆 提交于 2019-11-26 01:53:52
问题 Shouldn’t this definition be allowed in a lazy language like Haskell in which functions are curried? apply f [] = f apply f (x:xs) = apply (f x) xs It’s basically a function that applies the given function to the given list of arguments and is very easily done in Lisp for example. Are there any workarounds? 回答1: It is hard to give a static type to the apply function, since its type depends on the type of the (possibly heterogeneous) list argument. There are at least two ways one way to write

What does ** (double star/asterisk) and * (star/asterisk) do for parameters?

若如初见. 提交于 2019-11-26 01:17:19
问题 In the following method definitions, what does the * and ** do for param2 ? def foo(param1, *param2): def bar(param1, **param2): 回答1: The *args and **kwargs is a common idiom to allow arbitrary number of arguments to functions as described in the section more on defining functions in the Python documentation. The *args will give you all function parameters as a tuple: In [1]: def foo(*args): ...: for a in args: ...: print a ...: ...: In [2]: foo(1) 1 In [4]: foo(1,2,3) 1 2 3 The **kwargs will

Passing an array to a function with variable number of args in Swift

喜夏-厌秋 提交于 2019-11-26 00:54:17
问题 In The Swift Programming Language, it says: Functions can also take a variable number of arguments, collecting them into an array. func sumOf(numbers: Int...) -> Int { ... } When I call such a function with a comma-separated list of numbers (`sumOf(1, 2, 3, 4), they are made available as an array inside the function. Question: what if I already have an array of numbers that I want to pass to this function? let numbers = [1, 2, 3, 4] sumOf(numbers) This fails with a compiler error, “Could not

Why use the params keyword?

大兔子大兔子 提交于 2019-11-26 00:45:33
问题 I know this is a basic question, but I couldn\'t find an answer. Why use it? if you write a function or a method that\'s using it, when you remove it the code will still work perfectly, 100% as without it. E.g: With params: static public int addTwoEach(params int[] args) { int sum = 0; foreach (var item in args) sum += item + 2; return sum; } Without params: static public int addTwoEach(int[] args) { int sum = 0; foreach (var item in args) sum += item + 2; return sum; } 回答1: With params you

Can I pass an array as arguments to a method with variable arguments in Java?

邮差的信 提交于 2019-11-26 00:10:07
问题 I\'d like to be able to create a function like: class A { private String extraVar; public String myFormat(String format, Object ... args){ return String.format(format, extraVar, args); } } The problem here is that args is treated as Object[] in the method myFormat , and thus is a single argument to String.format , while I\'d like every single Object in args to be passed as a new argument. Since String.format is also a method with variable arguments, this should be possible. If this is not

Passing variable number of arguments around

冷暖自知 提交于 2019-11-26 00:08:44
问题 Say I have a C function which takes a variable number of arguments: How can I call another function which expects a variable number of arguments from inside of it, passing all the arguments that got into the first function? Example: void format_string(char *fmt, ...); void debug_print(int dbg_lvl, char *fmt, ...) { format_string(fmt, /* how do I pass all the arguments from \'...\'? */); fprintf(stdout, fmt); } 回答1: To pass the ellipses on, you have to convert them to a va_list and use that va

Is it possible to send a variable number of arguments to a JavaScript function?

邮差的信 提交于 2019-11-25 23:38:02
问题 Is it possible to send a variable number of arguments to a JavaScript function, from an array? var arr = [\'a\',\'b\',\'c\'] var func = function() { // debug alert(arguments.length); // for(arg in arguments) alert(arg); } func(\'a\',\'b\',\'c\',\'d\'); // prints 4 which is what I want, then \'a\',\'b\',\'c\',\'d\' func(arr); // prints 1, then \'Array\' I\'ve recently written a lot of Python and it\'s a wonderful pattern to be able to accept varargs and send them. e.g. def func(*args): print