variadic-functions

How can I create a function with a variable number of arguments?

一笑奈何 提交于 2019-11-29 15:57:30
问题 How can I create a function with a variable number of arguments in Rust? Like this Java code: void foo(String... args) { for (String arg : args) { System.out.println(arg); } } 回答1: In general, you can't - Rust does not support variadic functions, except when interoperating with C code that uses varargs. In this case, since all of your arguments are the same type, you can accept a slice: fn foo(args: &[&str]) { for arg in args { println!("{}", arg); } } fn main() { foo(&["hello", "world", "I",

Best Way to Store a va_list for Later Use in C/C++

拈花ヽ惹草 提交于 2019-11-29 14:44:37
问题 I am using a va_list to construct a string that is rendered. void Text2D::SetText(const char *szText, ...) This is all fine and good, but now the user has the ability to change the language while the application is running. I need to regenerate all the text strings and re-cache the text bitmaps after initialization. I would like to store the va_list and use it whenever the text needs to be generated. To give you some more background, this needs to happen in the case where the key string that

Why do Guava classes provide so many factory methods instead of just one that takes varargs? [duplicate]

天涯浪子 提交于 2019-11-29 13:18:44
Possible Duplicate: Why does Guava's ImmutableList have so many overloaded of() methods? Looking at Guava's ImmutableList (and some other classes), you'll find plenty of overloaded of convenience methods ("Returns an immutable list containing the given elements, in order.") that take a different number of parameters: ... public static <E> ImmutableList<E> of(E e1, E e2, E e3) public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4) public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5) ... All the way to this one: public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4,

Pass tuple's content as variadic function arguments

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 13:08:25
I play with C++0x for some time and now I want to use variadic templates and tuple to implement class "Task". I'm going to pass Task objects into newly created threads (using pthread). Task class will contain function pointer to function which should be called inside thread and arguments for this function, simplified code: class TaskCaller { // ... virtual bool dispatch (void); }; template<typename ...T_arguments> Task : public TaskCaller { public: // ... Task (bool (*function) (T_arguments&...), T_arguments... arguments) : function_arguments_tuple (arguments...), function (function) { // ...

Java: overloaded method resolution and varargs — confusing example

ⅰ亾dé卋堺 提交于 2019-11-29 12:15:49
问题 Just when I thought I understood JLS15.12 as it applied to varargs, here's this example: package com.example.test.reflect; public class MethodResolutionTest2 { public int compute(Object obj1, Object obj2) { return 42; } public int compute(String s, Object... objects) { return 43; } public static void main(String[] args) { MethodResolutionTest2 mrt2 = new MethodResolutionTest2(); System.out.println(mrt2.compute("hi", mrt2)); System.out.println(mrt2.compute("hi", new Object[]{mrt2})); System

When to prefer a varargs list to an array?

*爱你&永不变心* 提交于 2019-11-29 09:45:15
I'm implementing an API an have a method which you pass a list of paths where the program reads resources from public void importFrom(String... paths) { } I'm using varargs to make calling the method as convenient as possible to the user, like so obj.importFrom("/foo", "/foo/bar); Is this an appropriate use of varargs? Or is passing in an array better? In your case varargs is just fine. You don't really need to make an array of the paths that you will be importing because there's nothing you want to do with these paths other than to pass them along to your importFrom method. The varargs

Is it possible to have a variadic function in C with no non-variadic parameter?

时光怂恿深爱的人放手 提交于 2019-11-29 09:14:15
I have the following function: void doStuff(int unusedParameter, ...) { va_list params; va_start(params, unusedParameter); /* ... */ va_end(params); } As part of a refactor, I'd like to remove the unused parameter without otherwise changing the implementation of the function. As far as I can tell, it's impossible to use va_start when you don't have a last non-variadic parameter to refer to. Is there any way around this? Background: It is in fact a C++ program, so I could use some operator-overloading magic as suggested here , but I was hoping not to have to change the interface at this point.

How to use varargs in conjunction with function pointers in C on Win64?

喜夏-厌秋 提交于 2019-11-29 07:21:50
Consider the following C program: #include <stdio.h> #include <stdarg.h> typedef void (callptr)(); static void fixed(void *something, double val) { printf("%f\n", val); } static void dynamic(void *something, ...) { va_list args; va_start(args, something); double arg = va_arg(args, double); printf("%f\n", arg); } int main() { double x = 1337.1337; callptr *dynamic_func = (callptr *) &dynamic; dynamic_func(NULL, x); callptr *fixed_func = (callptr *) &fixed; fixed_func(NULL, x); printf("%f\n", x); } Basically, the idea is to store a function with variable arguments in a "generic" function pointer

How to write a variadic template recursive function?

 ̄綄美尐妖づ 提交于 2019-11-29 05:50:29
I'm trying to write a variadic template constexpr function which calculates sum of the template parameters given. Here's my code: template<int First, int... Rest> constexpr int f() { return First + f<Rest...>(); } template<int First> constexpr int f() { return First; } int main() { f<1, 2, 3>(); return 0; } Unfortunately, it does not compile reporting an error message error C2668: 'f': ambiguous call to overloaded function while trying to resolve f<3,>() call. I also tried to change my recursion base case to accept 0 template arguments instead of 1: template<> constexpr int f() { return 0; }

How to “pass on” a variable number of arguments to NSString's +stringWithFormat:

时光总嘲笑我的痴心妄想 提交于 2019-11-29 04:53:35
问题 I would like to write a function in Objective-C such as the one below, that takes a variable number of arguments, and passes those arguments on to +stringWithFormat: . I know about vsnprintf , but that would imply converting the NSString 'format' to C and back (and would also mean converting the formatting placeholders within it as well...). The code below compiles, but of course does not behave as I want :) NSString *estr(NSString *format, ...) { va_list args; va_start(args, format);