variadic-functions

Variable arguments in C, how to get values with a generic type?

守給你的承諾、 提交于 2019-11-26 21:34:05
问题 I'm trying to use C stdarg.h lib, with a generic type. The type int, is my generic type > to understand it, please, hold reading. So, my problem is: I have a function that accept variable number of arguments. like void function (int paramN, ...); In my program, there are no way to know, which is the type of variable arguments, it can be a char, an array, an int, an short, an function point, etc... like function (paramN, "Hey, I'm a string", 1, function_pint, array, -1); // -1 is a sentinel.

Functions with a flexible list of ordered/unordered and labeled/unlabeled inputs in MATLAB

给你一囗甜甜゛ 提交于 2019-11-26 21:26:43
问题 A lot of MATLAB functions have an input structure such as: output = function MyFun(a,b,c,'-setting1',s1,'-setting2',s2,'-setting3',s3) I am wondering how I should implement this kind of functionality in my own functions. To be precise, I would like to find out how I can create a function such that: The function has a variable number of inputs N + M The first N inputs are ordered and unlabeled . In the example above, N = 3 . The first input is always a , second input is always b , third input

Function with unknown number of parameters in C

十年热恋 提交于 2019-11-26 21:19:51
问题 How can I write (if it's possible at all...) a function which takes an unknown number of parameters in C99 (the return type is constant)? 回答1: Yes you can do it in C using what are referred to as Variadic Functions. The standard printf() and scanf() functions do this, for example. Put the ellipsis (three dots) as the last parameter where you want the 'variable number of parameters to be. To access the parameters include the <stdarg.h> header: #include <stdarg.h> And then you have a special

PHP: variable-length argument list by reference?

╄→гoц情女王★ 提交于 2019-11-26 21:08:48
问题 Is it possible to create a PHP function that takes a variable number of parameters all of them by reference? It doesn't help me a function that receives by reference an array of values nor a function that takes its arguments wrapped in an object because I'm working on function composition and argument binding. Don't think about call-time pass-by-reference either. That thing shouldn't even exist. 回答1: PHP 5.6 introduced new variadic syntax which supports pass-by-reference. (thanks @outis for

Ambiguous varargs methods

两盒软妹~` 提交于 2019-11-26 21:04:09
Here's a code example that doesn't compile: public class Test { public static void main(String[] args) { method(1); } public static void method(int... x) { System.out.println("varargs"); } public static void method(Integer... x) { System.out.println("single"); } } Can someone tell me the reason why these methods are ambiguous ? Thank you in advance. Consider the method signatures public static void foo(int a) and public static void foo(Integer a) Before boxing and unboxing, the call foo(1) would not have been ambiguous. To ensure compatibility with earlier versions of Java, the call remains

Java two varargs in one method

不打扰是莪最后的温柔 提交于 2019-11-26 20:52:55
Is there any way in java, to create a method, which is expecting two different varargs? I know, with the same object kind it isn't possible because the compiler does'nt know where to start or to end. But why it also is'nt possible with to different Object types? For example: public void doSomething(String... s, int... i){ //... //... } Is there any way to create method like this? Thank you! Only one vararg, sorry. But using asList() makes it almost as convenient: public void myMethod(List<Integer> args1, List<Integer> args2) { ... } ----------- import static java.util.Arrays.asList; myMethod

How to wrap a function with variable length arguments?

China☆狼群 提交于 2019-11-26 20:34:21
问题 I am looking to do this in C/C++. I came across Variable Length Arguments but this suggests a solution with Python & C using libffi. Now, if I want to wrap printf function with myprintf What I do is like below: void myprintf(char* fmt, ...) { va_list args; va_start(args,fmt); printf(fmt,args); va_end(args); } int _tmain(int argc, _TCHAR* argv[]) { int a = 9; int b = 10; char v = 'C'; myprintf("This is a number: %d and \nthis is a character: %c and \n another number: %d\n",a, v, b); return 0;

c++11 Variadic Printf performance

主宰稳场 提交于 2019-11-26 20:27:24
问题 variadic template is introduced in c++11. And I found the printf function can be replaced using it. However, cout is used in the implementation. I am wondering if it is possible to use something else to achieve type safe but not sacrifice too much performance. void safe_printf(const char *s) { while (*s) { if (*s == '%') { if (*(s + 1) == '%') { ++s; } else { throw "invalid format string: missing arguments"; } } std::cout << *s++; } } template<typename T, typename... Args> void safe_printf

How do you call an Objective-C variadic method from Swift?

心不动则不痛 提交于 2019-11-26 20:20:54
Supposing I have a class in Objective-c with a static method like this: + (NSError *)executeUpdateQuery:(NSString *)query, ...; How do I call that from Swift? The autocomplete doesn't recognise it, and the compiler is unhappy with: MyClassName.executeUpdateQuery("") Complaining that 'MyClassName.Type does not have a member named executeUpdateQuery' Write a va_list version of your variadic method; + (NSError *)executeUpdateQuery:(NSString *)query, ... { va_list argp; va_start(argp, query); NSError *error = [MyClassName executeUpdateQuery: query args:argp]; va_end(argp); return error; } +

Java varargs method param list vs. array

有些话、适合烂在心里 提交于 2019-11-26 19:28:50
问题 Varargs: public static void foo(String... string_array) { ... } versus Single array param: public static void bar(String[] string_array) { ... } Java 1.6 seems to accept/reject the following: String[] arr = {"abc", "def", "ghi"}; foo(arr); // accept bar(arr); // accept foo("abc", "def", "ghi"); // accept bar("abc", "def", "ghi"); // reject Assuming the above is true/correct, why not always use varargs instead of single array param? Seems to add a touch of caller flexiblity for free. Can an