variadic-functions

recursively concatenating a javascript functions arguments

孤人 提交于 2019-12-10 13:58:45
问题 I came across a javascript puzzle asking: Write a one-line piece of JavaScript code that concatenates all strings passed into a function: function concatenate(/*any number of strings*/) { var string = /*your one line here*/ return string; } @ meebo Seeing that the function arguments are represented as an indexed object MAYBE an array, i thought can be done in a recursive way. However my recursive implementation is throwing an error. --"conc.arguments.shift is not a function" -- function conc(

When should I use varargs in designing a Python API?

旧巷老猫 提交于 2019-12-10 13:38:42
问题 Is there a good rule of thumb as to when you should prefer varargs function signatures in your API over passing an iterable to a function? ("varargs" being short for "variadic" or "variable-number-of-arguments"; i.e. *args ) For example, os.path.join has a vararg signature: os.path.join(first_component, *rest) -> str Whereas min allows either: min(iterable[, key=func]) -> val min(a, b, c, ...[, key=func]) -> val Whereas any / all only permit an iterable: any(iterable) -> bool 回答1: Consider

C++ variable arguments with std::string only

寵の児 提交于 2019-12-10 13:34:16
问题 I'm trying to create a function that takes a variable amount of std::string arguments and formats a string with it. Example: Test::formatLine(const string::format, ...) { const std::string buffer; va_list args; va_start(args, format); vsprintf(buffer.c_str, format.c_str, args); va_end(args); cout << buffer << endl; } Compiling this snippet errors: Error 1 error C3867: 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str': function call missing argument list; use '&std:

Default arguments and variadic functions

非 Y 不嫁゛ 提交于 2019-12-10 12:39:47
问题 Is there any way to specify a default parameter in a variadic function?(Applies to templates also) 回答1: In C++ you can replace the variadic function with one based on the Named Parameter Idiom. See the C++ FAQ item 10.20 What is the "Named Parameter Idiom"?. That gives you default functionality & convenient notation. Cheers & hth., 回答2: Why would you need both variadic and default params? For example, myFunc(int a=5, int b=5, int c=5); can receive 0-3 parameters with default values, and

Ellipsis notation in C#?

六月ゝ 毕业季﹏ 提交于 2019-12-10 12:27:59
问题 Where can I get info about implementing my own methods that have the ellipsis notation, e.g. static void my_printf(char* format, ...) { } Also is that called ellipsis notation or is there a fancier name? 回答1: Have a look at the params keyword 回答2: From https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params: By using the params keyword, you can specify a method parameter that takes a variable number of arguments. You can send a comma-separated list of arguments of

How to tell a method has a varargs argument using reflection?

天大地大妈咪最大 提交于 2019-12-10 04:10:09
问题 Here is a sample code package org.example; import java.lang.reflect.Method; class TestRef { public void testA(String ... a) { for (String i : a) { System.out.println(i); } } public static void main(String[] args){ Class testRefClass = TestRef.class; for (Method m: testRefClass.getMethods()) { if (m.getName() == "testA") { System.out.println(m); } } } } The output is public void org.example.TestRef.testA(java.lang.String[]) So the signature of the method is reported to take a array of String.

How to add arguments to varargs? [duplicate]

那年仲夏 提交于 2019-12-10 03:34:20
问题 This question already has answers here : How to add new element to Varargs? (6 answers) Closed 6 years ago . Suppose I have methods void m1(Object... objs) { m2("added", objs); } and void m2(Object... objs) { for (Object o : objs) { // do something with Object o } } If I call m1("a", "b") , I'd like m2 to see an array of 3 Objects (Strings "added", "a" and "b"). However, instead m2 sees just 2 objects: String "added" and an Object[] array, which internally contains Strings "a" and "b". How

How call java vararg method from C with valist

一个人想着一个人 提交于 2019-12-10 03:26:38
问题 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! 回答1: 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 ..

Checking for varargs type ascription in Scala macros

久未见 提交于 2019-12-10 03:26:38
问题 Suppose I have this macro: import language.experimental.macros import scala.reflect.macros.Context object FooExample { def foo[A](xs: A*): Int = macro foo_impl[A] def foo_impl[A](c: Context)(xs: c.Expr[A]*) = c.literal(xs.size) } This works as expected with "real" varargs: scala> FooExample.foo(1, 2, 3) res0: Int = 3 But the behavior with a sequence ascribed to the varargs type is confusing to me (in Scala 2.10.0-RC3): scala> FooExample.foo(List(1, 2, 3): _*) res1: Int = 1 And to show that

Necessity of forward-declaring template functions

时光毁灭记忆、已成空白 提交于 2019-12-10 00:45:01
问题 I recently created this example code to illustrate C++11 variadic template function usage. template <typename Head, typename... Tail> void foo (Head, Tail...); template <typename... Tail> void foo (int, Tail...); void foo () {} template <typename... Tail> void foo (int x, Tail... tail) { std :: cout << "int:" << x; foo (tail...); } template <typename Head, typename... Tail> void foo (Head x, Tail... tail) { std :: cout << " ?:" << x; foo (tail...); } foo (int (123), float (123)); // Prints