variadic-functions

Using varargs from Scala

回眸只為那壹抹淺笑 提交于 2019-12-17 08:55:46
问题 I'm tearing my hair out trying to figure out how to do the following: def foo(msf: String, o: Any, os: Any*) = { println( String.format(msf, o :: List(os:_*)) ) } There's a reason why I have to declare the method with an o and an os Seq separately. Basically, I end up with the format method called with a single object parameter (of type List ). Attempting: def foo(msf: String, o: Any, os: Any*) = { println( String.format(msf, (o :: List(os:_*))).toArray ) } Gives me the type error: found:

Ambiguous varargs methods

余生长醉 提交于 2019-12-17 06:55:30
问题 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. 回答1: Consider the method signatures public static void foo(int a) and public static void foo(Integer a) Before boxing and unboxing, the call foo

Are there gotchas using varargs with reference parameters

北城以北 提交于 2019-12-17 06:45:48
问题 I have this piece of code (summarized)... AnsiString working(AnsiString format,...) { va_list argptr; AnsiString buff; va_start(argptr, format); buff.vprintf(format.c_str(), argptr); va_end(argptr); return buff; } And, on the basis that pass by reference is preferred where possible, I changed it thusly. AnsiString broken(const AnsiString &format,...) { ... the rest, totally identical ... } My calling code is like this:- AnsiString s1, s2; s1 = working("Hello %s", "World"); s2 = broken("Hello

Why does printf() promote a float to a double?

烈酒焚心 提交于 2019-12-17 03:40:49
问题 From a previous question: If you attempt to pass a float to printf , it'll be promoted to double before printf receives it printf() is a variadic function right? So does a variadic function promote a float argument to a double before passing it? 回答1: Yes, float arguments to variadic function are promoted to double. The draft C99 standard section 6.5.2.2 Function calls says: [...]and arguments that have type float are promoted to double. These are called the default argument promotions.[...]

How to pass an ArrayList to a varargs method parameter?

↘锁芯ラ 提交于 2019-12-17 03:26:10
问题 Basically I have an ArrayList of locations: ArrayList<WorldLocation> locations = new ArrayList<WorldLocation>(); below this I call the following method: .getMap(); the parameters in the getMap() method are: getMap(WorldLocation... locations) The problem I'm having is I'm not sure how to pass in the WHOLE list of locations into that method. I've tried .getMap(locations.toArray()) but getMap doesn't accept that because it doesn't accept Objects[]. Now if I use .getMap(locations.get(0)); it will

How to handle java variable length arguments in clojure?

佐手、 提交于 2019-12-17 03:19:11
问题 I'am wrapping a java lib into clojure, but i have problems dealing with variable length arguments. Say, TestClass.aStaticFunction(Integer... intList){/*....*/} How could i call this function in clojure? 回答1: Since Java varargs are actually arrays, you can call vararg functions in Clojure by passing an array. You could convert a Clojure seq (maybe by using Clojure's variety of variable argument functions) into an array: (TestClass/aStaticFunction (into-array Integer [(int 1),(int 2)])) or

varargs and the '…' argument

▼魔方 西西 提交于 2019-12-17 02:31:23
问题 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",

Clojure: How to Preserve Variadic Args Between Function Calls

[亡魂溺海] 提交于 2019-12-14 03:29:34
问题 I have two variadic functions. One of them passes its arguments to the other. The problem is that the varargs are becoming a list on the second call. How do I keep them varargs? => (defn foo [x & ys] (println x ys)) => (defn bar [x & ys] (foo (clojure.string/upper-case x) ys)) => (foo "hi") hi nil => (bar "hi") HI (nil) In the real function, foo passes its args to a variadic java function, so the varargs really need to stay varargs. How do I do this? 回答1: From http://clojuredocs.org/clojure

Python parameter list with single argument

白昼怎懂夜的黑 提交于 2019-12-14 03:25:56
问题 When testing Python parameter list with a single argument, I found some weird behavior with print . >>> def hi(*x): ... print(x) ... >>> hi() () >>> hi(1,2) (1, 2) >>> hi(1) (1,) Could any one explain to me what the last comma mean in hi(1) 's result (i.e. (1,) ) 回答1: Actually the behavior is only a little bit "weird." :-) Your parameter x is prefixed with a star, which means all the arguments you pass to the function will be "rolled up" into a single tuple, and x will be that tuple. The

Why is vsnprintf Not Writing the Same Number of Characters as strncpy Would?

╄→гoц情女王★ 提交于 2019-12-14 03:23:55
问题 I've asked the same question about strncpy, but there the string ends up containing the whole input string. When passing a string to vsnprintf the last character always gets chopped off: https://rextester.com/UIQMX91570 For simplicity I've also included the live example link above inline in the code: void bar(const char* format, va_list vlist) { const auto buf_size = vsnprintf(nullptr, 0U, format, vlist); string buffer(buf_size, '\0'); vsnprintf(data(buffer), buf_size, format, vlist); cout <<