variadic-functions

va_list and va_arg

帅比萌擦擦* 提交于 2020-01-13 03:52:47
问题 I using va_list like this: void foo(const char* firstArg, ...) { va_list args; va_start (args, firstArg); for (const char* arg = firstArg; arg != NULL; arg = va_arg(arg, const char*)) { // do something with arg } va_end(args); } foo("123", "234", "345") the first three arguments was passed to foo correctly, but where "345" is done, arg = va_arg(arg, const char*) set some other freak value to arg . so What's the problem. I using llvm3.0 as my compiler. 回答1: C does not automatically put a NULL

How to add new element to Varargs?

老子叫甜甜 提交于 2020-01-11 08:32:49
问题 I have a method public boolean findANDsetText (String Description, String ... extra ) { inside I want to call another method and pass it extras but I want to add new element (Description) to extras. object_for_text = getObject(find_arguments,extra); How can I do that in java? What would the code look like? I tired to accommodate code from this question but couldn't make it work. 回答1: extra is just a String array. As such: List<String> extrasList = Arrays.asList(extra); extrasList.add

Overloaded function as argument of variadic template function

邮差的信 提交于 2020-01-11 05:03:05
问题 I'm trying to make variadic template function, which takes as arguments overloaded function and its arguments :) int sumall(int a) { return a; } int sumall(int a, int b) { return a+b; } template<typename R, typename... A> R doit( R(*f)(A...), A... a) { return f(a...); } I want to call doit without any template specifiers nor casting: cout << doit(sumall, 7, 6) << endl That doesn't compile, but when return types are void, everything work perfect: void printsum(int a) { cout << a << endl; }

How do I fill a va_list

ぃ、小莉子 提交于 2020-01-09 10:16:40
问题 If I have a va_list I know how to extract all its elements: void printInts(int n,...) { va_list va; va_start(va, n); for(unsigned int i=0; i<n; i++) { int arg=va_arg(va, int); printf("%d",arg); } va_end(va); } So when I call printInts(3,1,2,3) the va_list get filled of all the parameters. But how do I manually fill a va_list without using va_start? I mean that I want something like: va_list va; push_arg(va, int, 5); // And so on until I fill all parameters ... I need this because there is a

how to pass variable arguments to another method?

佐手、 提交于 2020-01-09 02:14:47
问题 i have googled and came to know that how to use the variable arguments. but i want to pass my variable arguments to another method. i m getting errors. how to do that ? -(void) aMethod:(NSString *) a, ... { [self anotherMethod:a]; // i m doing this but getting error. how to pass complete vararg to anotherMethod } 回答1: AFAIK ObjectiveC (just like C and C++) do not provide you with a syntax that allows what you directly have in mind. The usual workaround is to create two versions of a function.

determine argument type from __VA_ARGS__ in compile time

吃可爱长大的小学妹 提交于 2020-01-04 08:15:38
问题 I wish to determine the types of the parameters passed to a function using VA_ARGS in order to route it to the right handler, but in compile time (and not inside a function with va_args()). by determine type i mean i need to know if the trace contains only integers or has strings in it as well, but i wish it will be in compile time. for example: #define TRACE_HANDLER(type_branch) (Invoke_ ## type_branch) #define TYPE_ARGS(args) ______//Determine if all arguments are uint32________ #define

Use variadic functions in C89 without passing number of arguments or a final argument?

天涯浪子 提交于 2020-01-04 05:41:06
问题 Let's say I have a variadic function foo(int tmp, ...) , when calling foo function I need to know how many arguments there are. I'm aware of two ways of finding out how many arguments there are: Use a final argument when calling foo, like -1, so your function call will be like this: foo(tmp, 1, 2, 9, -1) and when you are inside foo and a va_arg call returns -1 you know you have read all the function arguments Add one more argument in foo where the programmer will have the total number of

How to write a lazy, variable argument version of “orElse”

非 Y 不嫁゛ 提交于 2020-01-03 16:52:11
问题 Is it possible to write a generalised orElse method from Option that takes a variable number of arguments? That is, instead of: lazy val o1 = { println("foo"); None } lazy val o2 = { println("bar"); Some("bar") } lazy val o3 = { println("baz"); Some("baz") } // ... o1 orElse o2 orElse o3 // orElse ... You could use: orElse(o1, o2, o3) //, ... 回答1: According to the The Scala Language Specification (4.6 Function Declarations and Definitions) you cannot define varargs by-name parameters:

How to translate `void(*fn)(const char *, …)` to `std::function` and vice versa

半世苍凉 提交于 2020-01-03 15:42:40
问题 typedef void(*fn1)(const char *, ...); typedef std::function<void(const char *, ...)> fn2; // has initializer but incomplete type Intuitively, these are the effectively the same to me, but obviously my intuition is failing me. How would I reconcile these data types? How is fn2 an incomplete type? What changes are necessary to the signature of fn2 , to allow me to assign it a type of fn1 ? When creating a lambda to assign to fn2 , how do I access the variadic argument list? In other words,

How to wrap a call to a FFI function that uses VarArgs in Rust?

此生再无相见时 提交于 2020-01-03 14:25:06
问题 mexPrintf , just like printf , accepts a varargs list of arguments, but I don't know what the best way to wrap this is in Rust. There is a RFC for variadic generics, but what can we do today? In this example, I want to print of the number of inputs and outputs, but the wrapped function just prints garbage. Any idea how to fix this? #![allow(non_snake_case)] #![allow(unused_variables)] extern crate mex_sys; use mex_sys::mxArray; use std::ffi::CString; use std::os::raw::c_int; use std::os::raw: