variadic-functions

How to pass Scala array into Scala vararg method?

泄露秘密 提交于 2019-11-27 03:31:31
Consider the code below: private def test(some:String*){ } private def call () { val some = Array("asd", "zxc") test(some) } It prints expect String, found Array[String] Why? Are Scala varargs not arrays? Note I found several questions on Stack Overflow about Scala varargs, but all of them are about calling Java varargs methods or about converting Scala lists to arrays. Yuhuan Jiang Append :_* to the parameter in test like this test(some:_*) And it should work as you expect. If you wonder what that magical :_* does, please refer to this question . It is simple: def test(some:String*){} def

Inserting a variadic argument list into a vector?

这一生的挚爱 提交于 2019-11-27 02:05:10
问题 Forgive me if this has been answered already, as I couldn't find it... Basically I have an object that needs to take a variadic argument list in it's constructor and store the arguments in a vector. How do I initialize a vector from a the arguments of a variadic constructor? class GenericNode { public: GenericNode(GenericNode*... inputs) { /* Something like... */ // inputs_.push_back(inputs)...; } private: std::vector<GenericNode*> inputs_; }; 回答1: The best thing would be to use an

Generic functor for functions with any argument list

◇◆丶佛笑我妖孽 提交于 2019-11-27 02:01:57
问题 I need to implement a functor that takes any (!) function pointer when instantiated, analyses the argument types, stores the pointer and when operator() is called, does something with the pointer. The easiest case being, calling the function with its arguments. I tried converting the function pointer to something like std::function and I get the error: error: invalid use of incomplete type ‘struct std::function<void (*)(...)>’ /usr/include/c++/4.6/functional:1572:11: error: declaration of

Varargs in method overloading in Java

陌路散爱 提交于 2019-11-27 01:43:16
The following code doesn't compile. package varargspkg; public class Main { public static void test(int... i) { for (int t = 0; t < i.length; t++) { System.out.println(i[t]); } System.out.println("int"); } public static void test(float... f) { for (int t = 0; t < f.length; t++) { System.out.println(f[t]); } System.out.println("float"); } public static void main(String[] args) { test(1, 2); //Compilation error here quoted as follows. } } A compile-time error is issued. reference to test is ambiguous, both method test(int...) in varargspkg.Main and method test(float...) in varargspkg.Main match

Are there gotchas using varargs with reference parameters

孤人 提交于 2019-11-27 01:41:37
I have this piece of code (summarized)... AnsiString working(AnsiString format,...) { va_list argptr; AnsiString buff; va_start(argptr, format); buff.vprintf(format.c_str(), argptr); va_end(argptr); return buff; } And, on the basis that pass by reference is preferred where possible, I changed it thusly. AnsiString broken(const AnsiString &format,...) { ... the rest, totally identical ... } My calling code is like this:- AnsiString s1, s2; s1 = working("Hello %s", "World"); s2 = broken("Hello %s", "World"); But, s1 contains "Hello World", while s2 has "Hello (null)". I think this is due to the

C++ function call wrapper with function as template argument

天大地大妈咪最大 提交于 2019-11-27 01:29:27
问题 I'm trying to create a generic wrapper function that takes a function as a template argument and takes the same arguments as that function as its arguments. For example: template <typename F, F func> /* return type of F */ wrapper(Ts... Args /* not sure how to get Ts*/) { // do stuff auto ret = F(std::forward<Ts>(args)...); // do some other stuff return ret; } The solution needs to be castable to a function pointer with the same type as func so that I can pass it to a C api. In other words,

How are variable arguments implemented in gcc?

你说的曾经没有我的故事 提交于 2019-11-27 01:01:21
int max(int n, ...) I am using cdecl calling convention where the caller cleans up the variable after the callee returns. I am interested in knowing how do the macros va_end , va_start and va_arg work? Does the caller pass in the address of the array of arguments as the second argument to max? If you look at the way the C language stores the parameters on the stack, the way the macros work should become clear:- Higher memory address Last parameter Penultimate parameter .... Second parameter Lower memory address First parameter StackPointer -> Return address (note, depending on the hardware the

Java “params” in method signature?

做~自己de王妃 提交于 2019-11-27 00:45:26
In C#, if you want a method to have an indeterminate number of parameters, you can make the final parameter in the method signature a params so that the method parameter looks like an array but allows everyone using the method to pass as many parameters of that type as the caller wants. I'm fairly sure Java supports similar behaviour, but I cant find out how to do it. David Grant In Java it's called varargs , and the syntax looks like a regular parameter, but with an ellipsis ("...") after the type: public void foo(Object... bar) { for (Object baz : bar) { System.out.println(baz.toString()); }

How can Scala receive multiple parameters in a method definition?

我怕爱的太早我们不能终老 提交于 2019-11-27 00:43:11
问题 Java has: public void someMethod(int ... intArray) { // question: what is the equivalent to "..." // do something with intArray } how can I achieve the same functionality in Scala? That is, passing an undefined number of parameters to a method? 回答1: def someMethod(values : Int*) Gives an array. Put the variable argument parameter as the last formal parameter. 回答2: Both Java and Scala have varargs, and both only support it for the last parameters. def varargTest(ints:Int*) { ints.foreach

What exactly is va_end for? Is it always necessary to call it?

浪尽此生 提交于 2019-11-27 00:41:10
问题 va_end - Macro to reset arg_ptr . After accessing a variable argument list, the arg_ptr pointer is usually reset with va_end() . I understand that it is required if you want to re-iterate the list, but is it really needed if you aren't going to? Is it just good practice, like the rule "always have a default: in your switch "? 回答1: va_end is used to do cleanup. You don't want to smash the stack, do you? From man va_start : va_end() Each invocation of va_start() must be matched by a