variadic-functions

A few questions about legal arguments to printf(“%s”, …)

流过昼夜 提交于 2020-01-03 09:19:11
问题 I'm creating a modified printf implementation, and I'm not sure about the answers to these questions. Does zero work as a null string? (Is printf("%s", 0) allowed?) I'm guessing no, because 0 is an int . But then this prompts this question: Does NULL work as a null string? (Is printf("%s", NULL) allowed?) Logically, I think it should be yes, because NULL implies a pointer; but a lot of implementations seem to have #define NULL 0 , so I feel in practice it might be no. Which is correct? Does

How to pass multiple rows to PostgreSQL function?

断了今生、忘了曾经 提交于 2020-01-03 09:10:10
问题 How can we pass an array of (an unlimited amount of) rows (ie, a constant table) as the parameter/argument of a PostgreSQL function? Here's an idea: CREATE TYPE foo AS ( x bigint, y smallint, z varchar(64) ); CREATE OR REPLACE FUNCTION bar(bigint, foo[]) RETURNS TABLE(a bigint, x bigint, y smallint, z varchar(64)) AS $$ SELECT $1, x, y, z FROM unnest($2); $$ LANGUAGE SQL; The below function call works, but is there a way to make it shorter? SELECT * FROM bar(1, ARRAY[(1,2,'body1'),(2,1,'body2

Scala and SLF4J :: pass multiple parameters

自作多情 提交于 2020-01-03 08:48:13
问题 Having the following code: log.info("parameters {} and {}", param1, param2) compiles and works well with SLF4J in Scala However if I want to pass more arguments, I need to use Array: log.info("parameters {} and {} and {}", Array(param1, param2,param3)) which simply substitutes first parameter with array.toString and leaves rest of parameters unbound. The following code log.info("parameters {} and {} and {}", Array(param1, param2,param3) : _*) doesn't compile, because of: error: overloaded

Why does the use of … in any expression in a function cause the value of arg to be nil in Lua?

强颜欢笑 提交于 2020-01-03 08:20:10
问题 function tell(num,...) print("value of implicit table:",arg) --print("value of implicit table:",...) select(1,arg) --select(1,...) end tell(12,43,12,55) Why is it that using ... in an expression causes the value of arg to be nil e.g. with print("value of implicit table:",...) or select(1,...) ? 回答1: Lua 5.1 officially deprecates the use of the arg table for varargs, preferring ... . However, there is a compile time option for Lua itself, LUA_COMPAT_VARARG , to permit the use of arg in 5.1

Variadic functions and type-hinting in PHP

╄→尐↘猪︶ㄣ 提交于 2020-01-02 04:15:30
问题 Quick one: Is there any way to enforce types for variadic functions in PHP? I'm assuming not, however maybe I've missed something. As of now, I'm just forcing a single required argument of the needed type, and iterating to check the rest. public function myFunction(MyClass $object){ foreach(func_get_args() as $object){ if(!($object instanceof MyClass)){ // throw exception or something } $this->_objects[] = $object; } } Any better solutions? Purpose: A container object that acts as an iterated

In Java, why is the call foo() not ambigious given 2 varags methods foo(int… ints) and foo(Object… objects)?

那年仲夏 提交于 2020-01-02 01:12:15
问题 If I declare just the 2 varargs methods as follows: public void foo(String... strings) { System.out.println("Foo with Strings"); } and public void foo(int... ints) { System.out.println("Foo with ints"); } and then have the code: foo(); this is a compiler error due to the ambiguity as expected. However if I have just the following 2 versions of foo: public void foo(Object... objects) { System.out.println("Foo with Objects"); } and public void foo(int... ints) { System.out.println("Foo with

Why do Clojure variable arity args get different types depending on use?

匆匆过客 提交于 2020-01-02 00:49:16
问题 In answering another question I came across something I didn't expect with Clojure's variable arity function args: user=> (defn wtf [& more] (println (type more)) :ok) #'user/wtf ;; 1) user=> (wtf 1 2 3 4) clojure.lang.ArraySeq :ok ;; 2) user=> (let [x (wtf 1 2 3 4)] x) clojure.lang.ArraySeq :ok ;; 3) user=> (def x (wtf 1 2 3 4)) clojure.lang.PersistentVector$ChunkedSeq #'user/x user=> x :ok Why is the type ArraySeq in 1) and 2), but PersistentVector$ChunkedSeq in 3)? 回答1: Short answer: It's

Objective-c : Accessing variadic arguments in method [duplicate]

故事扮演 提交于 2020-01-01 17:00:17
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to create variable argument methods in Objective-C Variable number of method parameters in Objective C - Need an example Following is an example of a method having variadic arguments. - (void)numberOfParameters:group,... { NSLog(@"%@",group); } In above method, I know to access the first one of the variadic arguments. Would you please help me for accessing the others as well? I am just going through ObjC.pdf

Converting NSArray Contents to a varargs (With ARC) For Use With NSString initWithFormat

99封情书 提交于 2020-01-01 08:12:09
问题 We have some code today that takes an NSArray and passes it as a argument list to -[NSString initWithFormat:arguments] and we're trying to get this to work with ARC. Here's the code were using NSString* format = @"Item %s and Item %s"; // Retrieved elsewhere NSArray* args = [NSArray arrayWithObjects:@"1", @"2", nil]; // Retrieved elsewhere // http://cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html char* argsList = (char*) malloc(sizeof(NSString*) * args.count); [args getObjects

SBRM/RAII for std::va_list/va_start()/va_end use

穿精又带淫゛_ 提交于 2020-01-01 05:20:11
问题 My code contains snippets like these: std::va_list ap; va_start(ap, msgfmt); snprintf_buf buf; const tchar * msg = buf.print_va_list(msgfmt, ap); va_end(ap); These are short and va_start() and va_end() are close together so they are not much of a problem. Exceptions from calls in between the two could be a problem (or not?). Simple test shows that calling va_start() from a function without ellipsis is not allowed. Is calling va_end() from a different function than va_start() was called from