variadic-functions

When varargs started to not conflict with no-arg?

百般思念 提交于 2019-12-11 04:04:14
问题 Today I found that the following code compiles and runs with no any warning: public class Try_MultipleArguments2 { public static void main(String[] args) { myfunction(); myfunction(1, 2, 3); } public static void myfunction(int ... as) { System.out.println("varags called"); } public static void myfunction() { System.out.println("noarg called"); } } I am remembering clear, that it was not so earlier. Is this JVM change or my memory glitch??? How it distinguish between no-arg and varargs? UPDATE

How to use an array of arrays with array_map(…) in PHP?

自闭症网瘾萝莉.ら 提交于 2019-12-11 02:35:41
问题 The PHP function array_map(...) expects a callback as first parameter (or null for creating an array of arrays) and a variable number of array arguments, e.g.: $foo => array_map(null, $bar, $buz); Now I have a case, where I need to pass to array_map(...) a variable number of arrays. I cannot hard-code this, since the arrays for the array_map(...) 's input are generated dynamically. function performSomeLogicAndGetArgumentsForMyFunction() { ... return ['bar' => [...], 'buz' => [...]]; } $foo =

Does C varargs use a keyword called 'end'?

拥有回忆 提交于 2019-12-11 01:55:22
问题 I have a lot of code that uses C style variable arguments. The code passes in a variable called end at the very end of our variable length function calls. And.... the code also has an enumerator called end . So far they haven't clashed (compiler error says it has an ambiguous definition: It won't tell me where the mysterious second 'end' is defined) until I changed to the VC 10.0 compiler (VS 2010). So is end some sort of reserved keyword used especially in variable args? I know very little

Getting the type of passed argument in variadic function

二次信任 提交于 2019-12-10 23:23:34
问题 Is there any trick to get the type of the passed in arguments without explicitly stating the type in a string as one of the argument? To add on the post just now, basically what I want to do is if is type A call functionA else if is type B call functionB. Could variadic template solve that? Thanks. 回答1: No, if they may vary in type, you need to pass some kind of structure specifying what those types are. Also note that only certain types are allowed, and you're best off restricting it to just

Using a function with variable argument strings

怎甘沉沦 提交于 2019-12-10 22:28:56
问题 I was playing around a bit with functions with variable arguments, and decided to make a function to create vectors with the arguments. My function for creating an int vector worked... vector<int> makeIntVector(int numArgs, ...) { va_list listPointer; va_start(listPointer, numArgs); vector<int> made; for(int a = 0; a < numArgs; a++) made.push_back(va_arg(listPointer, int)); va_end(listPointer); return made; } but not my function for creating a string vector: vector<string> makeStringVector

Nulls in variadic C functions

我怕爱的太早我们不能终老 提交于 2019-12-10 20:17:06
问题 If I defined a variadic function: #include <stdio.h> #include <stdarg.h> int f(char*s,...) { va_list ap; int i=0; va_start(ap, s); while(s) { printf("%s ", s); i++; s=va_arg(ap,char*); } va_end(ap); return i; } int main() { return f("a","b",0); } gcc (linux x64) compiles this and the exe runs and prints "a b ". is there any need for a cast like: return f("a","b",(char*)0) on common systems? 回答1: Your code should work fine on any system that supports the SYSV x64 ABI standard. It may work on

Pass a function with undetermined number of arguments and call it with the variadic arguments

半城伤御伤魂 提交于 2019-12-10 17:52:11
问题 I'd like to create a function "lazy" which accepts a function with an undetermined number of arguments as parameter. What type do I need or which casts have to be done? Then I want to execute that thing later in function "evaluate". How do I then pass the arguments I passed before to the "lazy" function to the passed function pointer? Some code to illustrate my problem: char *function_I_want_to_call(void *foo, type bar, ...); // the arguments are unknown to lazy() and evaluate() typedef

possible buffer overflow vulnerability for va_list in C?

佐手、 提交于 2019-12-10 15:49:57
问题 I have the following code: int ircsocket_print(char *message, ...) { char buffer[512]; int iError; va_list va; va_start(va, message); vsprintf(buffer, message, va); va_end(va); send(ircsocket_connection, buffer, strlen(buffer), 0); return 1; } And I wanted to know if this code is vulerable to buffer overflows by providing char arrays with a size > 512 to the variables list? And if so - How can I fix this? thank you. 回答1: Yes, it is vulnerable. Simply use vsnprintf instead: vsnprintf(buffer,

Overloading the End of Recursion for a Variable Length Template Function

时光怂恿深爱的人放手 提交于 2019-12-10 14:58:13
问题 François Andrieux gave me a good workaround for this Visual Studio 2017 problem. I was trying to build on his answer like so: template<class T, size_t N> ostream& vector_insert_impl(ostream& lhs, const char*, const T& rhs) { return lhs << at(rhs, N); } template<class T, size_t N, size_t... I> ostream& vector_insert_impl(ostream& lhs, const char* delim, const T& rhs) { return vector_insert_impl<T, I...>(lhs << at(rhs, N) << delim, delim, rhs); } template <typename T, size_t... I> ostream&

How to invoke a MethodHandle with varargs

↘锁芯ラ 提交于 2019-12-10 14:44:12
问题 I'm trying to replace a reflective invocation with a MethodHandle, but varargs seem to be impossible to deal with. My reflective invoker currently looks like this: public class Invoker { private final Method delegate; public Invoker(Method delegate) { this.delegate = delegate; } public Object execute(Object target, Object[] args) { return delegate.invoke(target, args); } } My current attempt at rewriting it looks like this (the interface the Invoker exposes has to stay the same): public class