variadic-functions

An example of use of varargs in C

假如想象 提交于 2019-11-26 12:23:30
Here I found an example of how varargs can be used in C. #include <stdarg.h> double average(int count, ...) { va_list ap; int j; double tot = 0; va_start(ap, count); //Requires the last fixed parameter (to get the address) for(j=0; j<count; j++) tot+=va_arg(ap, double); //Requires the type to cast to. Increments ap to the next argument. va_end(ap); return tot/count; } I can understand this example only to some extent. It is not clear to me why we use va_start(ap, count); . As far as I understand, in this way we set the iterator to its first element. But why it is not set to the beginning by

varargs and the &#39;…&#39; argument

主宰稳场 提交于 2019-11-26 12:23:21
Consider the method declaration: String.format(String, Object ...) The Object ... argument is just a reference to an array of Object s. Is there a way to use this method with a reference to an actual Object array? If I pass in an Object array to the ... argument - will the resultant argument value be a two-dimensional array - because an Object[] is itself an Object : Object[] params = ....; // Make the array (for example based on user-input) String s = String.format("%S has %.2f euros", params); So the first component of the array (Which is used in the String.format method), will be an array

What default promotions of types are there in the variadic arguments list?

被刻印的时光 ゝ 提交于 2019-11-26 12:19:38
问题 For example, I use printf function in C++ for 8-bit CPU (AVR). Is the following code safe: uint8_t a = 5; printf(\"%d\", a); Here %d expects int (16-bit in my case, and at least 16-bit in any case), but I pass 8-bit integer. Does C/C++ standards guarantee that any type with rank lesser than int promoted to int ? The same question for float a and %f that expects double , and other analogous types. 回答1: Look in the draft n1256 (C99 with Technical corrigenda TC1, TC2, and TC3 included) for 6.5.2

How to pass variable number of arguments to a PHP function

落花浮王杯 提交于 2019-11-26 12:09:30
I have a PHP function that takes a variable number of arguments (using func_num_args() and func_get_args() ), but the number of arguments I want to pass the function depends on the length of an array. Is there a way to call a PHP function with a variable number of arguments? If you have your arguments in an array, you might be interested by the call_user_func_array function. If the number of arguments you want to pass depends on the length of an array, it probably means you can pack them into an array themselves -- and use that one for the second parameter of call_user_func_array . Elements of

How to pass variable number of arguments to printf/sprintf

丶灬走出姿态 提交于 2019-11-26 11:37:55
I have a class that holds an "error" function that will format some text. I want to accept a variable number of arguments and then format them using printf. Example: class MyClass { public: void Error(const char* format, ...); }; The Error method should take in the parameters, call printf/sprintf to format it and then do something with it. I don't want to write all the formatting myself so it makes sense to try and figure out how to use the existing formatting. void Error(const char* format, ...) { va_list argptr; va_start(argptr, format); vfprintf(stderr, format, argptr); va_end(argptr); } If

trailing return type using decltype with a variadic template function

只谈情不闲聊 提交于 2019-11-26 11:22:12
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. However, with more arguments than this, I get errors that sum() is simply not defined yet. If I

How do I define Lisp’s apply in Haskell?

心已入冬 提交于 2019-11-26 10:34:45
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? Don Stewart 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 this function in Haskell that I can think of: Using reflection We can defer type checking of the

Varargs in method overloading in Java

那年仲夏 提交于 2019-11-26 09:44:53
问题 The following code doesn\'t compile. package varargspkg; public class Main { public static void test(int... i) { for (int t = 0; t < i.length; t++) { System.out.println(i[t]); } System.out.println(\"int\"); } public static void test(float... f) { for (int t = 0; t < f.length; t++) { System.out.println(f[t]); } System.out.println(\"float\"); } public static void main(String[] args) { test(1, 2); //Compilation error here quoted as follows. } } A compile-time error is issued. reference to test

Java “params” in method signature?

落爺英雄遲暮 提交于 2019-11-26 09:29:18
问题 In C#, if you want a method to have an indeterminate number of parameters, you can make the final parameter in the method signature a params so that the method parameter looks like an array but allows everyone using the method to pass as many parameters of that type as the caller wants. I\'m fairly sure Java supports similar behaviour, but I cant find out how to do it. 回答1: In Java it's called varargs, and the syntax looks like a regular parameter, but with an ellipsis ("...") after the type:

Java two varargs in one method

谁说我不能喝 提交于 2019-11-26 09:05:29
问题 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! 回答1: Only one vararg, sorry. But using asList() makes it almost as convenient: public void myMethod