variadic-functions

Passing an ellipsis to another variadic function [duplicate]

一曲冷凌霜 提交于 2019-11-26 15:42:01
问题 This question already has an answer here: Forward an invocation of a variadic function in C 10 answers I have approximately 30 variadic functions. Each one accepts a path as the final argument, e.g.: bool do_foo(struct *f, int q, const char *fmt, ...) In each function, I have to check that the expanded format is less then or equal to a certain size. So, I find myself copy / pasting the same chunk of code to check for how many characters vsnprintf() didn't print, set errno accordingly and bail

How to handle java variable length arguments in clojure?

前提是你 提交于 2019-11-26 15:26:44
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? 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 (defn a-static-function-wrapper [& args] (TestClass/aStaticFunction (into-array Integer args)) Or make an array

C++11: Number of Variadic Template Function Parameters?

帅比萌擦擦* 提交于 2019-11-26 15:25:14
问题 How can I get a count of the number of arguments to a variadic template function? ie: template<typename... T> void f(const T&... t) { int n = number_of_args(t); ... } What is the best way to implement number_of_args in the above? 回答1: Just write this: const std::size_t n = sizeof...(T); //you may use `constexpr` instead of `const` Note that n is a constant expression (i.e known at compile-time), which means you may use it where constant expression is needed, such as: std::array<int, n> a; /

Calling Java varargs method with single null argument?

无人久伴 提交于 2019-11-26 15:22:20
If I have a vararg Java method foo(Object ...arg) and I call foo(null, null) , I have both arg[0] and arg[1] as null s. But if I call foo(null) , arg itself is null. Why is this happening? How should I call foo such that foo.length == 1 && foo[0] == null is true ? The issue is that when you use the literal null, Java doesn't know what type it is supposed to be. It could be a null Object, or it could be a null Object array. For a single argument it assumes the latter. You have two choices. Cast the null explicitly to Object or call the method using a strongly typed variable. See the example

Spread Syntax vs Rest Parameter in ES2015 / ES6

落花浮王杯 提交于 2019-11-26 15:10:05
问题 I am confused about the spread syntax and rest parameter in ES2015. Can anybody explain the difference between them with proper examples? 回答1: When using spread, you are expanding a single variable into more: var abc = ['a', 'b', 'c']; var def = ['d', 'e', 'f']; var alpha = [ ...abc, ...def ]; console.log(alpha)// alpha == ['a', 'b', 'c', 'd', 'e', 'f']; When using rest arguments, you are collapsing all remaining arguments of a function into one array: function sum( first, ...others ) { for (

Populating a va_list

五迷三道 提交于 2019-11-26 14:29:10
问题 Is there a way to create a va_list from scratch? I'm trying to call a function that takes a va_list as a parameter: func(void **entry, int num_args, va_list args, char *key); ...from a function that doesn't take a variable number of arguments. The only way I can think of is to create an intermediary function that takes varargs and then passing along its va_list, which is pretty stupid: void stupid_func(void **entry, char *key, int num_args, ...) { va_list args; va_start(args, num_args); func

Why doesn&#39;t autoboxing overrule varargs when using method overloading in Java 7?

◇◆丶佛笑我妖孽 提交于 2019-11-26 14:14:09
问题 We have a class LogManager in our Java project which looks like this: public class LogManager { public void log(Level logLevel, Object... args) { // do something } public void log(Level logLevel, int value, Object... args) { // do something else } } When compiling the project with OpenJDK 6 under Debian everyting works fine. When using OpenJDK 7 the build (done with ant) produces the following errors and the build fails: [javac] /…/LogManager.java:123: error: reference to log is ambiguous,

Possible heap pollution via varargs parameter

久未见 提交于 2019-11-26 14:06:15
I understand this occurs with Java 7 when using varargs with a generic type; But my question is.. What exactly does Eclipse mean when it says "its use could potentially pollute the heap?" And How does the new @SafeVarargs annotation prevent this? Heap pollution is a technical term. It refers to references which have a type that is not a supertype of the object they point to. List<A> listOfAs = new ArrayList<>(); List<B> listOfBs = (List<B>)(Object)listOfAs; // points to a list of As This can lead to "unexplainable" ClassCastException s. // if the heap never gets polluted, this should never

How to pass Scala array into Scala vararg method?

浪子不回头ぞ 提交于 2019-11-26 12:38:58
问题 Consider the code below: private def test(some:String*){ } private def call () { val some = Array(\"asd\", \"zxc\") test(some) } It prints expect String, found Array[String] Why? Are Scala varargs not arrays? Note I found several questions on Stack Overflow about Scala varargs, but all of them are about calling Java varargs methods or about converting Scala lists to arrays. 回答1: Append :_* to the parameter in test like this test(some:_*) And it should work as you expect. If you wonder what

How to properly match varargs in Mockito

一世执手 提交于 2019-11-26 12:24:39
I've been trying to get to mock a method with vararg parameters using Mockito: interface A { B b(int x, int y, C... c); } A a = mock(A.class); B b = mock(B.class); when(a.b(anyInt(), anyInt(), any(C[].class))).thenReturn(b); assertEquals(b, a.b(1, 2)); This doesn't work, however if I do this instead: when(a.b(anyInt(), anyInt())).thenReturn(b); assertEquals(b, a.b(1, 2)); This works, despite that I have completely omitted the varargs argument when stubbing the method. Any clues? topchef Mockito 1.8.1 introduced anyVararg() matcher : when(a.b(anyInt(), anyInt(), Matchers.<String>anyVararg()))