variadic-functions

How to pass variable number of arguments from one function to another?

大城市里の小女人 提交于 2019-12-06 05:20:48
问题 Is there any way to directly pass a variable number of arguments from one function to another? I'd like to achieve a minimal solution like the following: int func1(string param1, ...){ int status = STATUS_1; func2(status, param1, ...); } I know I can do this using something like the following, but this code is going to be duplicated multiple times so I'd like to keep it as minimalist as possible while also keeping the function call very short int func1(string param1, ...){ int status = STATUS

Is a pointer to a function which have unknown number of parameters possible?

谁说我不能喝 提交于 2019-12-06 04:33:37
I am writing a simple class to measure performance of a function in terms of time . The user should be able to send a pointer to his function, parameters of the function, times to call the function and i will call the function, return the time elapsed. Here my problem is I dont know how many parameters the user's function takes! I thought to use variadic functions to get unknown number of parameters, however I still have the problem of declaring the function pointer that user passes as a parameter(because it doesnt have constant variable number) and not knowing the types of variables I recieve

Pushing and popping the first element of a std::tuple

China☆狼群 提交于 2019-12-06 04:07:18
I am writing a function in C++ with a variable number of arguments (and different types) in this way template<typename ...Ts> void myFunction(Ts ...args) { //create std::tuple to access and manipulate single elements of the pack auto myTuple = std::make_tuple(args...); //do stuff return; } What i would like to do, but I don't know how, is to push and pop elements from the tuple, in particular the first element... something like //remove the first element of the tuple thereby decreasing its size by one myTuple.pop_front() //add addThis as the first element of the tuple thereby increasing its

Why is varargs always the last parameter in a method signature?

和自甴很熟 提交于 2019-12-06 02:36:58
问题 Why does varargs have to be the last parameter in method signature? I want to know the reason. 回答1: Because it makes the compiler's life simpler. There's no real reason why it couldn't have more arguments after, but it would require a much more complex compiler and so the spec was written that way. 回答2: The main reason is because it would be potentially ambiguous otherwise.... For example, how could the compiler tell whether arguments are varargs or separate named arguments in a long list of

Swizzling a method with variable arguments and forward the message - Bad Access

萝らか妹 提交于 2019-12-06 02:26:30
问题 I'm implementing a "Code Injector Class", that through method swizzling can give you the possibility to do something like this: FLCodeInjector *injector = [FLCodeInjector injectorForClass:[self class]]; [injector injectCodeBeforeSelector:@selector(aSelector:) code:^{ NSLog(@"This code should be injected"); }]; aSelector can be a method with variable number of arguments, and variable return type. Arguments / and return type can be objects or primitive type. First, I attach the code of

Unpacking Argument List with Variadic Template

我怕爱的太早我们不能终老 提交于 2019-12-06 02:17:17
问题 I'm trying to create a C++ convenience wrapper around an old C-API that uses an opaque data type. There's one particular C-function which takes a format string, along with a variable number of arguments using the C <stdarg.h> facilities. As part of my wrapper, I want to be able to pass an arbitrary number of arguments (including C++ objects) to this function. However, since obviously the <stdarg.h> facilities can't work with non-POD data, I created a templated conversion function which

OCaml function with variable number of arguments

本秂侑毒 提交于 2019-12-06 02:13:59
问题 I'm exploring "advanced" uses of OCaml functions and I'm wondering how I can write a function with variable number of arguments. For example, a function like: let sum x1,x2,x3,.....,xn = x1+x2,+x3....+xn 回答1: With a bit of type hackery, sure: let sum f = f 0 let arg x acc g = g (acc + x) let z a = a And the (ab)usage: # sum z;; - : int = 0 # sum (arg 1) z;; - : int = 1 # sum (arg 1) (arg 2) (arg 3) z;; - : int = 6 Neat, huh? But don't use this - it's a hack. For an explanation, see this page

Magnet pattern with repeated parameters (varargs)

≯℡__Kan透↙ 提交于 2019-12-05 23:35:38
问题 Is it possible to use the magnet pattern with varargs: object Values { implicit def fromInt (x : Int ) = Values() implicit def fromInts(xs: Int*) = Values() } case class Values() object Foo { def bar(values: Values) {} } Foo.bar(0) Foo.bar(1,2,3) // ! "error: too many arguments for method bar: (values: Values)Unit" ? 回答1: As already mentioned by gourlaysama, turning the varargs into a single Product will do the trick, syntactically speaking: implicit def fromInts(t: Product) = Values() This

How to define a variadic function

那年仲夏 提交于 2019-12-05 22:42:21
问题 I'm looking for something similar to Javascript's arguments array: function parent(){ child.apply(this.arguments); } I'm aware of the dot notation for variable argument lengths and also scheme's apply function. This doesn't seem to work as the dot is taken to be the first argument: (define (parent .) (list .)) (parent 1 3 4 6 7) Error: bad argument count - received 5 but expected 1: #<procedure (array arg-list)> This works but isn't ideal. I'd like to call the function without the extra

Varargs with null parameters in Java

五迷三道 提交于 2019-12-05 22:00:57
问题 The following code simply uses a null reference as a varargs parameter. package currenttime; import java.util.Arrays; public class Main { private static void temp(String...str) { System.out.println(Arrays.asList(str)); } public static void main(String[] args) { temp(null,null); temp(null); } } The first call to the method temp(null, null); displays [null, null] means that str[0]=null and str[1]=null . but the later call to temp(null); causes the NullPointerException to be thrown which appears