variadic-functions

By-name repeated parameters

感情迁移 提交于 2019-11-27 06:46:12
问题 How to pass by-name repeated parameters in Scala? The following code fails to work: scala> def foo(s: (=> String)*) = { <console>:1: error: no by-name parameter type allowed here def foo(s: (=> String)*) = { ^ Is there any other way I could pass a variable number of by name parameters to the method? 回答1: This isn't very pretty but it allows you to pass byname parameters varargs style def printAndReturn(s: String) = { println(s) s } def foo(s: (Unit => String)*) { println("\nIn foo") s foreach

How to pass elements of an arrayList to variadic function

非 Y 不嫁゛ 提交于 2019-11-27 06:35:22
问题 I've got an arrayList filled with elements. I would like to pass the elements of that array list as arguments to a variadic function. My function public SequenceEntityModifier(final IEntityModifier... pEntityModifiers) My ArrayList ArrayList<IEntityModifier> arr = new ArrayList<IEntityModifier>(); arr.add(new MoveXModifier(1, 50, 120)); arr.add(new MoveXModifier(1, 120, 50)); I'd like to pass it to the function as if I would pass them individually. new SequenceEntityModifier( /* elements of

Is a variable length argument treated as an array in Java?

蓝咒 提交于 2019-11-27 05:54:49
问题 As I understand an array consists of fixed number of elements and a variable length argument takes as many number of arguments as you pass (of the same type). But are they same? Can I pass one where the other is expected? 回答1: Yes, if you have a method with a varargs parameter like this: public void foo(String... names) and you call it like this: foo("x", "y", "z"); then the compiler just converts that into: foo(new String[] { "x", "y", "z"}); The type of the names parameter is String[] , and

How to implement “Variadic Template” with pre-c++0x(VS2008)?

安稳与你 提交于 2019-11-27 05:45:48
问题 I'm using Visual Studio 2008, and I want to implement string formatting function without Variable Argument List . How to implement "Variadic Template" with pre-c++0x(VS2008)? Is there any library which implements this like boost? Or another way to implement this? Here is my sample code. (of course, this can't be complied because i'm using VS2008.) bool VarPrint(std::ostringstream& out, const std::string& s) { std::string::size_type offset = 0; if((offset = s.find("%")) != std::string::npos) {

Passing NON-POD type to Variadic function is undefined behavior?

。_饼干妹妹 提交于 2019-11-27 04:45:48
问题 In this document, the author said Only a POD-type can be an argument for the ellipsis "..." while std::string is not a POD-type. I'm understanding this as Passing NON-POD type to Variadic function is undefined behavior . Is it right? Though, is he saying C/C++ standard? I tried to find it at n3242 C++ spec. But can not find. I'd like to know I'm understanding rightly and this is a standard. 回答1: It's specified in C++11 5.2.2/7: Passing a potentially-evaluated argument of class type having a

C: Unspecified number of parameters - void foo()

落爺英雄遲暮 提交于 2019-11-27 04:37:38
问题 I read here that in C void foo() means a function foo taking an unspecified number of arguments of unspecified type . Can anyone give me or point me to an example where a C function takes an unspecified number of arguments? What can this be applied to in C ? I couldn't find anything on the web. Thanks! 回答1: That's an old-style function declaration. This declaration: void foo(); declares that foo is a function returning void that takes an unspecified but fixed number and type(s) of arguments.

What is the type of a variable-length argument list in Scala?

元气小坏坏 提交于 2019-11-27 04:30:08
问题 Suppose that I declare a function as follows: def test(args: String*) = args mkString What is the type of args ? 回答1: This is called a variable number of arguments or in short varargs. It's static type is Seq[T] where T represents T* . Because Seq[T] is an interface it can't be used as an implementation, which is in this case scala.collection.mutable.WrappedArray[T]. To find out such things it can be useful to use the REPL: // static type scala> def test(args: String*) = args test: (args:

how to pass variable arguments to another method?

放肆的年华 提交于 2019-11-27 04:11:14
i have googled and came to know that how to use the variable arguments. but i want to pass my variable arguments to another method. i m getting errors. how to do that ? -(void) aMethod:(NSString *) a, ... { [self anotherMethod:a]; // i m doing this but getting error. how to pass complete vararg to anotherMethod } AFAIK ObjectiveC (just like C and C++) do not provide you with a syntax that allows what you directly have in mind. The usual workaround is to create two versions of a function. One that may be called directly using ... and another one called by others functions passing the parameters

Varargs Java Ambiguous Call

霸气de小男生 提交于 2019-11-27 04:09:00
I'm a little confused about Java's varargs methods: public static int sum(int ...a) { return 0; } public static double sum(double ...a) { return 0.0; } When I tried to invoke sum() without passing any argument, then the int version of method was invoked. I don't understand why; normally the compiler must raise an error. By contrast, the following piece of code generates a compiler error when I try to invoke sum without any argument: public static int sum(int ...a) { return 0; } public static boolean sum(boolean ...a) { return true; } The general rule that applies here is this: if one method

Is GCC mishandling a pointer to a va_list passed to a function?

人走茶凉 提交于 2019-11-27 04:00:41
问题 The question 'Pass va_list or pointer to va_list?' has an answer which quotes the standard (ISO/IEC 9899:1999 - §7.15 'Variable arguments <stdarg.h> , footnote 212) as explicitly saying that: It is permitted to create a pointer to a va_list and pass that pointer to another function, in which case the original function may make further use of the original list after the other function returns. I'm compiling some code which can be exemplified by the following (the real code is very considerably