variadic-functions

Variable argument function ambiguity

不问归期 提交于 2019-12-01 01:56:23
问题 public static void main(String[] args) { System.out.println(fun(2,3,4)); } static int fun(int a,int b,int c) { return 1; } static int fun(int ... a) { return 0; } Output: 1 Question: In the above case why does the function fun select the 1st function and not the second.On what basis is the selection done since there is no way to determine which fun the user actually wanted to call ? 回答1: Basically there's a preference for a specific call. Aside from anything else, this means it's possible to

Java: Find out whether function was called with varargs or array

六眼飞鱼酱① 提交于 2019-12-01 01:37:31
问题 Is there a way to find out whether a Java function (or a constructor) that takes varargs was actually called with varargs or with an array? Say I have the following: public class MyCompositeObjects { MyObject[] objects; MyCompositeObjects(MyObjects... objects) { this.objects = Arrays.copyOf(objects,objects.length); // or just: this.objects = objects; ? } // ... } The constructor may be called with a single MyObject[] argument, which may change later, and if I do not copy the array in the

Is the 1st argument of an Objective C variadic function mandatory?

依然范特西╮ 提交于 2019-12-01 01:30:52
问题 Here is an example of a variadic function in Obj C. // This method takes an object and a variable number of args - (void) appendObjects:(id) firstObject, ...; Is it really mandatory to have the first argument as an Obj C object? If not, what should be the syntax? EDIT: Thanks for your responses - the first argument does not need to be an NSObject , but what I meant to ask is: Is it possible to do away with the first argument altogether? I probably did not frame the question well the first

Passing variadic template arguments to a variadic function

梦想与她 提交于 2019-12-01 00:18:22
问题 We are using a third-party C library which provides a printf() -style log function, void log(const char *format, ...); For reasons that aren't worth going in to, we need to limit the rate at which messages get logged, something along the lines of void rate_limited_log(const char* format, ...) { if (<not too fast>) { log(format, ...); } } Fortunately the authors of the C library knew what they were doing, and provided void logv(const char* format, va_list ap); so writing the above function is

Cython equivalent of c define #define myfunc(node x,…) SetNode(x.getattributeNode(),__VA_ARGS__)

99封情书 提交于 2019-11-30 23:47:20
Cython equivalent of c define #define myfunc(Node x,...) SetNode(x.getattributeNode(),__VA_ARGS__) I have a c api SetNode which takes first argument a node of struct type node and N variables (N is variable number from 0-N) here is a c example to solve such problum exampleAPI.c #include<stdarg.h> float sumN(int len,...){ va_list argp; int i; float s=0; va_start(argp,len); for(i=0;i<len;i++){ s+=va_arg(argp,int); } va_end(argp); } exampleAPI.h #include<stdarg.h> float sumN(int len,...) examplecode.c #include<stdarg.h> #include"exampleAPI.h" int len(float first,...){ va_list argp; int i=1; va

How do I get a formatted NSString from format and va_list?

时光毁灭记忆、已成空白 提交于 2019-11-30 23:43:05
问题 I'm developing a static library that will be distributed to other developers, who may need debug statements. So I have several levels of logging. In order to avoid constant appearance of if(loggingLevelCurrentlySet >= loggingLevelWantedForThisInstance){ NSLog(@"log this"); } I created a set of logging function wrappers. A simplified version looks like this: void myLog(int logLevel, NSString *format, va_list args){ if((loggingLevelCurrentlySet >= logLevel)){ NSLogv(format, args); } } void

Which integral promotions do take place when printing a char?

限于喜欢 提交于 2019-11-30 23:40:29
问题 I recently read that unsigned char x=1; printf("%u",x); invokes undefined behaviour since due to the format specifier %u, printf expects an unsigned int. But still I would like to understand what is going on in this example. I think that the integral promotion rules apply with the expression printf("%u",x) and the value represented by x . A.6.1 Integral Promotion A character, a short integer, or an integer bit-field, all either signed or not, or an object of enumeration type, may be used in

How can arguments to variadic functions be passed by reference in PHP?

谁都会走 提交于 2019-11-30 22:31:09
问题 Assuming it's possible, how would one pass arguments by reference to a variadic function without generating a warning in PHP? We can no longer use the '&' operator in a function call, otherwise I'd accept that (even though it would be error prone, should a coder forget it). What inspired this is are old MySQLi wrapper classes that I unearthed (these days, I'd just use PDO). The only difference between the wrappers and the MySQLi classes is the wrappers throw exceptions rather than returning

Variadic list constructor, how to default to the correct type and get type safety

China☆狼群 提交于 2019-11-30 20:51:33
Here's what I've got: {-# LANGUAGE MultiParamTypeClasses , FlexibleInstances #-} class ListResultMult r a where lstM :: a -> [a] -> r listM :: ListResultMult r a => a -> r listM a = lstM a [] instance ListResultMult r a => ListResultMult (a -> r) a where lstM a as x = lstM x $ a:as instance ListResultMult [a] a where lstM a as = reverse $ a:as Here's how it works: > listM 'a' 'b' 'c' :: String "abc" > putStrLn $ listM 'a' 'b' 'c' abc > listM (1::Int) (2::Int) :: [Int] [1,2] Here's how it fails > sum $ listM 1 2 No instance for (ListResultMult (a2 -> [a0]) a1) ... > listM 1 :: [Int] No instance

PHP variable length arguments?

让人想犯罪 __ 提交于 2019-11-30 20:35:44
In Python and others, there's special syntax for variable length argument lists: def do_something(*args): # do something do_something(1, 2, 3, 4, 5, ...) # arbitrarily long list I was reading the PHP manual, and it said this: PHP 4 and above has support for variable-length argument lists in user-defined functions. This is really quite easy, using the func_num_args(), func_get_arg(), and func_get_args() functions. No special syntax is required, and argument lists may still be explicitly provided with function definitions and will behave as normal. I get the first part. You can pass as many