variadic-functions

Is it possible to pass va_list to variadic template?

随声附和 提交于 2019-12-05 21:02:00
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 { virtual ~ArgsPackBase() {} }; template<typename... Args> struct ArgsPack : public ArgsPackBase {

Forward variadic arguments for a UIAlertView

可紊 提交于 2019-12-05 18:22:52
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 doing that: - (id)initWithTitle:(NSString *)title message:(NSString*)message delegate:(id /*

Variadic function without named argument

*爱你&永不变心* 提交于 2019-12-05 17:42:24
I noticed, that both GCC and MSVC are happy with the following code: #include <iostream> void foo(...); int main() { foo(); } void foo(...) { std::cout << "foo\n"; } More specifically, code was run under GCC 6.2.0 and Visual Studio 2015. I know that C requires at least one named parameter preceding the ellipsis, which allows to handle any number of arguments using specialized va_start , va_args , and va_end macros from <stdarg.h> (here <cstdarg> ) header. Otherwise, it won't even compile. Does C++ have some special treatment for "pure ellipsis" form or is it not suitable for fetching the

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

℡╲_俬逩灬. 提交于 2019-12-05 16:43:43
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 double g(double a, double b, double c) { return a + b + c; } Array<double,2> x, y, z;. x.map(g, y, z); won

Platform inconsistencies with vsprintf and va_list

左心房为你撑大大i 提交于 2019-12-05 14:25:15
Background: I am currently trying to "extend" standard C formatting with support for handling a certain struct, similar to how Objective-C extends C formatting to allow support for NSString with the "%@" sequence. The one problem I'm struggling with is that vsprintf seems to be behaving differently on OS X versus Linux (I've tested with Ubuntu 10.10 and 12.04). On OS X, it is behaving how I thought it should, where after calling vsprintf, calling va_arg returns the ms pointer (as if the vsprintf function called va_arg to get the 5). On Linux, however, the va_list does not change from vsprintf,

C++ variadic template function parameter with default value

夙愿已清 提交于 2019-12-05 13:20:22
问题 I have a function which takes one parameter with a default value. Now I also want it to take a variable number of parameters and forward them to some other function. Function parameters with default value have to be last, so... can I put that parameter after the variadic pack and the compiler will detect whether I'm supplying it or not when calling the function? (Assuming the pack doesn't contain the type of that one last parameter. If necessary, we can assume that, because that type is

Determining if an Objective-C method is variadic during runtime

你离开我真会死。 提交于 2019-12-05 10:35:54
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? 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 the stock main() ): strings asdfasdfasdf Boogity methodWithOneIntArg: variadicMethodWithIDSentinel: v20@0:8i16

In C++, do variadic functions (those with … at the end of the parameter list) necessarily follow the __cdecl calling convention?

杀马特。学长 韩版系。学妹 提交于 2019-12-05 05:42:43
I know that __stdcall functions can't have ellipses, but I want to be sure there are no platforms that support the stdarg.h functions for calling conventions other than __cdecl or __stdcall. The calling convention has to be one where the caller clears the arguments from the stack (because the callee doesn't know what will be passed). That doesn't necessarily correspond to what Microsoft calls "__cdecl" though. Just for example, on a SPARC, it'll normally pass the arguments in registers, because that's how the SPARC is designed to work -- its registers basically act as a call stack that gets

Is there an easier alternative to mimicking the splat operator?

本小妞迷上赌 提交于 2019-12-05 04:47:31
I've found it's available in Ruby, but I recognize it from what I've done in Python; the "splat" operator. Long story short, I'm wondering if there's a simpler way to accomplish what I currently am, mimicking what the "splat" operator does. I made a central method that the rest can call because I realized I have several very similar ones, and they were all doing the same except for a few minor things. Here's the method signature: private String callScript(String scriptLocation, String... extraArgs) throws Exception { I want to require at least one argument (the scriptLocation ), and then allow

How call java vararg method from C with valist

主宰稳场 提交于 2019-12-05 03:59:46
I have C method with varargs void message(int id, ...) And it should call Java static method with vararg public static void message(String messageName, String ... args) How can I do it using C valist and JNI API? Thank you! Your not likely to get any use out of passing the address of a va_list up to Java. Here are a few snippets showing how to pass up an array using "..." and va_list. Add error/exception/return checking as necessary. In Java: static private void javaDefineArray(Object ... args) { for (Object o : args) { print("javaDefineArray " + o); } } In C: // Lookup the method using