variadic-functions

Is a pointer to a function which have unknown number of parameters possible?

妖精的绣舞 提交于 2019-12-22 13:31:47
问题 I am writing a simple class to measure performance of a function in terms of time . The user should be able to send a pointer to his function, parameters of the function, times to call the function and i will call the function, return the time elapsed. Here my problem is I dont know how many parameters the user's function takes! I thought to use variadic functions to get unknown number of parameters, however I still have the problem of declaring the function pointer that user passes as a

Marshalling ArgIterator to va_list

那年仲夏 提交于 2019-12-22 13:01:08
问题 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

Is it possible to pass va_list to variadic template?

最后都变了- 提交于 2019-12-22 11:01:23
问题 I know that va_list is usually something you should avoid since its not very safe, but is it possible to pass the arguments from a function like: void foo(...); to a function like template<typename... Args> void bar(Args... arguments); ? edit: Originally I wanted to try to use this to call a virtual function with a variable amount of arguments / types, but this was not the way to go making this question kind of irrelevant. Eventually I ended up doing something like this: struct ArgsPackBase {

Forward variadic arguments for a UIAlertView

ぃ、小莉子 提交于 2019-12-22 10:44:10
问题 I'm trying to set up a very simple UIAlertView with a text edit, an Ok and a cancel button, and I want to disable the Ok button based on the content of the text edit. To be able to retain the delegate so that he doesn't go away before the alert view (and thus cause a crash as soon as the user does something with the alert view), I've subclassed it. Now, I want to be able to forward the otherButtonTitles argument from my init method to the UIAlertView init method, but for some reasons, simply

How to iterate over std::index_sequence

旧街凉风 提交于 2019-12-22 09:49:00
问题 I have this code in my source: template <std::size_t... Dims> class DimensionPack { public: using Dimensions = std::index_sequence<Dims...>; static const std::size_t total_dimensions = sizeof...(Dims); std::vector<unsigned int> even_or_odd; public: DimensionPack() { unsigned idx = 0; for ( ; idx < total_dimensions; idx++ ) { //MatrixDimensionOddOrEven mdoe( Dimensions[idx] ); //unsigned int val = mdoe.even_or_odd; //even_or_odd.push_back( val ); } } }; The commented lines of code is the code

How to make variadic template class method take function pointer as argument with type derived from function template?

允我心安 提交于 2019-12-22 08:55:32
问题 Sorry the title is a mouthful. I'm working on an array class similar to the one discussed here. I want to define a "map" function that takes a user-defined function and applies it to each element of the array. For the purposes of type-checking, I'd like to define it such that the user-specified function must take the same number of arguments as are passed to the map function, so that double f(double a, double b) { return a + b; } Array<double,2> x, y, z; x.map(f, y, z); will compile but

Determining if an Objective-C method is variadic during runtime

断了今生、忘了曾经 提交于 2019-12-22 07:53:30
问题 Is there a way to find out -- at runtime -- whether a given method is of variadic type? Something like method_getTypeEncoding() ; that won't tell me whether a method accepts variable number of arguments. Or is there maybe a trick to tell so? 回答1: Robert's comment is correct. Consider: @interface Boogity @end @implementation Boogity - (void)methodWithOneIntArg:(int)a {;} - (void)variadicMethodWithIDSentinel:(id)a, ... {;} @end Running strings on the resulting binary produces (there was also

Determining if an Objective-C method is variadic during runtime

Deadly 提交于 2019-12-22 07:52:22
问题 Is there a way to find out -- at runtime -- whether a given method is of variadic type? Something like method_getTypeEncoding() ; that won't tell me whether a method accepts variable number of arguments. Or is there maybe a trick to tell so? 回答1: Robert's comment is correct. Consider: @interface Boogity @end @implementation Boogity - (void)methodWithOneIntArg:(int)a {;} - (void)variadicMethodWithIDSentinel:(id)a, ... {;} @end Running strings on the resulting binary produces (there was also

Why List<Integer[]> listOfArrays = Arrays.asList(new Integer[]{1, 2}) doesn't compile? [duplicate]

久未见 提交于 2019-12-22 07:00:10
问题 This question already has answers here : How to create a List of Object arrays that has only one array? [duplicate] (3 answers) Closed 3 years ago . 1) OK List<int[]> listOfArrays1 = Arrays.asList( new int[]{1, 2} ); 2) OK List<int[]> listOfArrays2 = Arrays.asList( new int[]{1, 2}, new int[]{3, 4} ); 3) Compile error Type mismatch: cannot convert from List<Integer> to List<Integer[]> List<Integer[]> listOfArrays3 = Arrays.asList( new Integer[]{1, 2} ); 4) OK List<Integer[]> listOfArrays4 =

Why can't we just use arrays instead of varargs?

假装没事ソ 提交于 2019-12-22 04:01:57
问题 I just came across varargs while learning android( doInBackground(Type... params) ) , SO posts clarified the use of it My question is why can't we just use Arrays instead of varargs public void foo(String...strings) { } I can replace this type of a call by packing my variable number of arguments in an array and passing it to a method such as this public void foo(String[] alternativeWay){ } Also does main(String[] args) in java use varargs , if not how are we able to pass runtime parameters to