variadic-functions

Variable number of arguments (va_list) with a function callback?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 13:43:01
I am working on implementing a function that would execute another function a few seconds in the future, depending upon the user's input. I have a priority queue of a class (which I am calling TimedEvent) that contains a function pointer to the action I want it to execute at the end of the interval. Say for instance that the user wants the program to call a function "xyz" after 3 seconds, they would create a new TimedEvent with the time and the function pointer to xyz and add it to the priority queue (which is sorted by time, with the soonest events happening first). I have been able to

Clojure: Transforming Varargs But Keeping them Varargs

☆樱花仙子☆ 提交于 2019-12-06 11:59:33
I am working on a little pet project in Clojure. I have a function that I pass varargs: (defn foor [bar & args] (let new-args (custom-transform args)] (do-something new-args)))))) But, do-something is expecting varargs, not a list object. In custom-transform (defn custom-transform [& args] (if vars vars nil)) How do I preserve the "vararg"-iness of my args after applying a transformation? I think apply should do the trick: (apply do-something new-args) Your problem isn't specific to varargs. What you want to do is to call do-something , passing the elements of an array as the arguments to the

How would I 'generate variadic parameters'?

半城伤御伤魂 提交于 2019-12-06 11:43:25
问题 I need a way to pass a variable amount of parameters to a function in this circumstance: template<typename ...T> struct Lunch { Lunch(T...){} }; template<typename T> T CheckLuaValue(lua_State* luaState,int index) { //Do Stuff return value; } template <class MemberType, typename ReturnType, typename... Params> struct MemberFunctionWrapper <ReturnType (MemberType::*) (Params...)> { static int CFunctionWrapper (lua_State* luaState) { ReturnType (MemberType::*)(Params...) functionPointer =

Objective-C va_list and selectors

瘦欲@ 提交于 2019-12-06 11:35:51
问题 Is it possible to use @selector and performSelector: (or similar) with methods using variable arguments list? I'm writing a class that can be assigned a delegate to override the default behavior. In the presence of a delegate select method calls made on an instance of that class will be forward to the same corresponding delegate method, some which use variable argument lists. So, for instance, I need to be able to create retrieve SEL reference and message the delegate object with a method

C++ how to use ellipsis without a preceding argument

三世轮回 提交于 2019-12-06 11:35:50
Hi I have a class with a member function that takes a variable number of arguments. The class knows how many arguments to expect once it is instantiated . e.g class myClass { myClass(int num_args){na=num_args;}; private: int na; public: void do_something(int num_args, ...); } void myClass::do_something(int num_args, ...) { va_list vl; va_start(vl, num_args); for(int i=0; i < num_args; i++) do_anotherthing(va_arg(vl, type)); va_end } so i end up calling as follows: myClass A(5); myClass B(4); A.do_something(5, a, b, c, d, e); B.do_something(4, a, b, c, d); It seems untidy to me to have to keep

Variadic function (va_arg) doesn't work with float, while printf does? What the difference is?

这一生的挚爱 提交于 2019-12-06 10:13:19
问题 I just happened to have similar situation like in this question from two years: Variadic function (va_arg) doesn't work with float? There is said that the problem is promoting float to double when we call things like va_arg(arg, float) My question is at the end of this post, but first let's take a look at @Jack's answer below the question linked above: #include <stdio.h> #include <stdarg.h> void foo(int n, ...) { va_list vl; va_start(vl, n); int c; double val; for(c = 0; c < n; c++) { val =

How to use varargs as the parameter of Constructor.getConstructor( ) in java

蓝咒 提交于 2019-12-06 09:50:47
I have a java class like below which I want to create an instance of this class dynamically by using class name. class Demo { public Demo(String... s) { //some initialization here. } } And I want to create an object using following code Class<?> klass = Class.forName("Demo"); Constructor<?> con = klass.getConstructor("**what should be here**"); Object obj = con.newInstance(param1, param2, ...); String... is just String[] so you can use Constructor<?> con = klass.getConstructor(String[].class); Note that you need to invoke the constructor like Object o = con.newInstance((Object) new String[] {

Can stdcall have a variable arguments?

落花浮王杯 提交于 2019-12-06 08:20:55
问题 As far as I know, only the caller-clean-stack convention can use variable arguments. By the way, the WinApi StringCchPrintfW is declared like this.(I removed the SAL) __inline HRESULT __stdcall StringCchPrintfW( STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszFormat, ... ); Can stdcall have a variable arguments either? 回答1: No. The stdcall calling convention has the callee clean the stack. Since the callee is cleaning the stack there is no way for it to know at compile time how

Marshalling ArgIterator to va_list

五迷三道 提交于 2019-12-06 07:47:31
So, I got an idea to try to p/invoke C functions that take va_list . I know how to p/invoke classic varargs functions (with __arglist ), and as it seemed to me that va_list works just like an ArgIterator , I thought it might be possible to just pass it to the method: [DllImport("msvcrt.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] public static extern int vswprintf(StringBuilder str, int length, string format, ArgIterator args); public static int vswprintf(StringBuilder str, int length, string format, __arglist) { return vswprintf(str, length, format, new

How do I deal with Function<T, R> and ellipsis/varargs in this case?

自闭症网瘾萝莉.ら 提交于 2019-12-06 06:18:46
问题 One of my project is throwing-lambdas; in it I aim to ease the use of potential @FunctionalInterface s in Stream s, whose only "defect" for being used in streams is that they throw checked exceptions (on my part I'd rather call defective the fact that you can't throw checked exceptions from streams but that's another story). So, for Function<T, R> I define this: @FunctionalInterface public interface ThrowingFunction<T, R> extends Function<T, R> { R doApply(T t) throws Throwable; default R