variadic-functions

Count number of parameters in C variable argument method call

馋奶兔 提交于 2019-12-01 05:12:14
When using va_start(), va_arg() and va_end() to read parameters passed to a method, is there a way to count how many arguments there are? According to the man page if you call va_arg() too many times you get "random errors": If there is no next argument, or if type is not compatible with the type of the actual next argument (as promoted according to the default argument promotions), random errors will occur. No. a Variable Argument function (such as printf ), must "know" when to stop looking for more arguments. printf knows by the number of %d , %s and other symbols in its format string. Other

Unpacking slice of slices

可紊 提交于 2019-12-01 04:50:44
问题 I am curious about unpacking a slice of slices and sending them as arguments to a variadic function. Let's say we have a function with variadic parameters: func unpack(args ...interface{}) If we wan't to pass in a slice of interfaces it works, it doesn't matter if we unpack it or not: slice := []interface{}{1,2,3} unpack(slice) // works unpack(slice...) // works It gets tricky if we have a slice of slices. Here the compiler doesn't let us pass in an unpacked version: sliceOfSlices := [][

How can I pass an array as parameters to a vararg function?

我的梦境 提交于 2019-12-01 03:29:22
I have some code that looks like this: uint8_t activities[8]; uint8_t numActivities = 0; ... activities[numActivities++] = someValue; ... activities[numActivities++] = someOtherValue; ... switch (numActivities) { 0 : break; 1 : LogEvent(1, activities[0]); break; 2 : LogEvent(1, activities[0], activities[1]); break; 3 : LogEvent(1, activities[0], activities[1], activities[2]); break; // and so on } where LogEvent() is a varargs function. Is there a more elgant way to do this? [Update] Aplogies to @0x69 et al. I omitted to say that there are many cases where LogEvent() could not take an array as

How do I get a formatted NSString from format and va_list?

泄露秘密 提交于 2019-12-01 03:23:18
I'm developing a static library that will be distributed to other developers, who may need debug statements. So I have several levels of logging. In order to avoid constant appearance of if(loggingLevelCurrentlySet >= loggingLevelWantedForThisInstance){ NSLog(@"log this"); } I created a set of logging function wrappers. A simplified version looks like this: void myLog(int logLevel, NSString *format, va_list args){ if((loggingLevelCurrentlySet >= logLevel)){ NSLogv(format, args); } } void myLogLevel1(NSString *format, ...){ va_list args; va_start(args, format); myLog(1, format, args); va_end

Using varargs in a Tag Library Descriptor

一笑奈何 提交于 2019-12-01 03:21:40
Is it possible to have a TLD map to the following function: public static <T> T[] toArray(T... stuff) { return stuff; } So that I can do: <c:forEach items="${my:toArray('a', 'b', 'c')}"... I tried the following <function-signature> s java.lang.Object toArray( java.lang.Object... ) java.lang.Object[] toArray( java.lang.Object[] ) And others but nothing seems to work. Unfortunately that's not possible. The EL resolver immediately interprets the commas in the function as separate arguments without checking if there are any methods taking varargs. Your best bet is using JSTL fn:split() instead. <%

How can arguments to variadic functions be passed by reference in PHP?

这一生的挚爱 提交于 2019-12-01 03:13:52
Assuming it's possible, how would one pass arguments by reference to a variadic function without generating a warning in PHP? We can no longer use the '&' operator in a function call, otherwise I'd accept that (even though it would be error prone, should a coder forget it). What inspired this is are old MySQLi wrapper classes that I unearthed (these days, I'd just use PDO). The only difference between the wrappers and the MySQLi classes is the wrappers throw exceptions rather than returning FALSE . class DBException extends RuntimeException {} ... class MySQLi_throwing extends mysqli { ...

va_list has not been declared

时光怂恿深爱的人放手 提交于 2019-12-01 03:06:01
When compiling some working code on Fedora 11, I am getting this error: /usr/include/c++/4.4.1/cstdarg:56: error: ‘::va_list’ has not been declared I am using: [doriad@davedesktop VTK]$ g++ --version g++ (GCC) 4.4.1 20090725 (Red Hat 4.4.1-2) Does anyone know what the problem could be? I had the same error message and I solved including one of the next files #include <stdarg.h> or #include <cstdarg> Bringing in the varadic macro set in g++ 4.4 has confusing and twisted semantics. You might get a better idea of what isn't happening by using g++ -E broken_code.cpp and looking at what the pre

varargs(va_list va_start) doesn't work with pass-by-reference parameter [duplicate]

雨燕双飞 提交于 2019-12-01 02:44:27
Possible Duplicate: Are there gotchas using varargs with reference parameters Hi, I have a problem with varargs. Look at my code(Microsoft Visual Studio 2005 or 2008). #include <stdarg.h> struct Test { int a; }; void T1(int n, ...) { va_list args; va_start(args, n); char* p = va_arg(args, char*); va_end(args); } void T2(Test n, ...) { va_list args; va_start(args, n); char* p = va_arg(args, char*); va_end(args); } void T3(const Test& n, ...) { va_list args; va_start(args, n); char* p = va_arg(args, char*); // p corrupt!! va_end(args); } int _tmain(int argc, _TCHAR* argv[]) { const Test t; T1(1,

Count number of parameters in C variable argument method call

安稳与你 提交于 2019-12-01 02:36:18
问题 When using va_start(), va_arg() and va_end() to read parameters passed to a method, is there a way to count how many arguments there are? According to the man page if you call va_arg() too many times you get "random errors": If there is no next argument, or if type is not compatible with the type of the actual next argument (as promoted according to the default argument promotions), random errors will occur. 回答1: No. a Variable Argument function (such as printf ), must "know" when to stop

Requiring at least one element in java variable argument list

醉酒当歌 提交于 2019-12-01 02:10:38
In this code construct: public MyClass(Integer... numbers) { do_something_with(numbers[]); } is it possible to require that numbers contains at least one entry in such a way, that this is checked at compile-time? (At run-time, of course, I can just check numbers.length.) Clearly I could do this: public MyClass(Integer number, Integer... more_numbers) { do_something_with(number, more_numbers[]); } but this isn't going to be very elegant. The reason I would like to do this is to make sure that a sub-class does not simply forget to call this constructor at all, which will default to a call to