variadic-functions

c++ non-type parameter pack expansion

白昼怎懂夜的黑 提交于 2019-12-07 09:22:51
问题 I am writing template function that is parametrized by single type, and has variable number of parameters of the same type (not of different types). It should check if first value is among the rest. I wanted to write it like this: #include <unordered_set> template <typename T> static bool value_in(T val, T vals...) { // compiles, but uses only vals[0]: const std::unordered_set<T> allowed {vals}; // error: pack expansion does not contain any unexpanded parameter packs: // const std::unordered

Typed kwargs in Julia

≯℡__Kan透↙ 提交于 2019-12-07 07:05:18
问题 Is it possible to type function kwargs in Julia? The following works for standard Vararg s. function int_args(args::Integer...) args end int_args(1, 2, 3) # (1, 2, 3) int_args(1, 2, 3.0) # ERROR: MethodError: `int_args` has no method matching int_args(::Int64, ::Int64, ::Float64) However, when applying this same syntax to kwargs, all function calls seem to error. function int_kwargs(; kwargs::Integer...) kwargs end int_kwargs(x=1, y=2) # ERROR: MethodError: `__int_kwargs#0__` has no method

C varargs - va_copy issues

主宰稳场 提交于 2019-12-07 03:58:53
问题 I'm writing a function in C that takes a variable number of arguments. size_t myprintf(char *fmt, ...); So far, so good. I've decided it's best to do things the Right Way™ and make a version that takes variable arguments, and another version that takes a va_list . size_t myprintf(char *fmt, ...); size_t myvprintf(char *fmt, va_list args); Not that hard to do. Except my_vprintf() needs to send its args out to two different functions (first to snprintf() with a length of 0 to determine how much

Is there an easier alternative to mimicking the splat operator?

我怕爱的太早我们不能终老 提交于 2019-12-07 02:21:28
问题 I've found it's available in Ruby, but I recognize it from what I've done in Python; the "splat" operator. Long story short, I'm wondering if there's a simpler way to accomplish what I currently am, mimicking what the "splat" operator does. I made a central method that the rest can call because I realized I have several very similar ones, and they were all doing the same except for a few minor things. Here's the method signature: private String callScript(String scriptLocation, String...

How to pass vararg as array to function in Kotlin?

邮差的信 提交于 2019-12-07 00:53:39
问题 I want to pass vararg from the buy function to the drive function but I get a compile error: required Array<T> found Array<out T> code: class Car fun buy(vararg cars: Car) { drive(cars) //compile error } fun drive(cars: Array<Car>) { //... } 回答1: Another solution would be to change drive to fun drive(Array<out Car>) { ... } . This of course means that the cars inside drive cannot be modified but avoids the copying. 回答2: The precise error is: Type mismatch. Required: Array<Car> Found: Array

In C++, do variadic functions (those with … at the end of the parameter list) necessarily follow the __cdecl calling convention?

寵の児 提交于 2019-12-07 00:22:58
问题 I know that __stdcall functions can't have ellipses, but I want to be sure there are no platforms that support the stdarg.h functions for calling conventions other than __cdecl or __stdcall. 回答1: The calling convention has to be one where the caller clears the arguments from the stack (because the callee doesn't know what will be passed). That doesn't necessarily correspond to what Microsoft calls "__cdecl" though. Just for example, on a SPARC, it'll normally pass the arguments in registers,

Java: varargs in interface

穿精又带淫゛_ 提交于 2019-12-06 20:55:27
public interface MyInterface { public ArrayList<Double> f(ArrayList<Double>... args); } I get the warning: Type safety: Potential heap pollution via varargs parameter paramOfChildren . I can suppress this warning with @SuppressWarnings("unchecked") . However, in every class that implements this interface, I get this warning again. I wonder if there's a way to suppress this warning once for all the classes implementing the interface. If not, is there a good reason that we shouldn't do it? you can pin down the generic: class DoubleList extends ArrayList<Double> { private static final long

Scala variadic functions and Seq

℡╲_俬逩灬. 提交于 2019-12-06 18:33:26
问题 As far as I know, traits like List or Seq are implemented in the Scala standard library instead of being part of the language itself. There is one thing that I do not understand, though: one has a syntax for variadic functions that looks like def foo(args: String*) = ... Internally one has access to args and it will be a Seq . It is not clear to me whether: Seq is considered a special data structure enough to appear as part of the language, or the * notation here is a particular case of a

Will `params` in C# always cause a new array to be allocated on every call?

扶醉桌前 提交于 2019-12-06 17:55:16
问题 C#/.NET has variadic function parameters by passing an Array type by-reference (as opposed to C/C++ which just places all of the values directly on the stack, for better and for worse). In the C# world this has a neat advantage of allowing you to call the same function with either 'raw' arguments or a reusable array instance: CultureInfo c = CultureInfo.InvariantCulture; String formatted0 = String.Format( c, "{0} {1} {2}", 1, 2, 3 ); Int32 third = 3; String formatted0 = String.Format( c, "{0}

An array of Strings vs String Varargs

限于喜欢 提交于 2019-12-06 15:13:36
问题 What is the difference between void method(String[] a) and void method(String... a) ? The first method take an array of Strings where as the second method takes one or more String arguments. What are the different features that they provide? Moreover, don't know why but this is working: public class Test { public static void main(String[] args) { String[] a = {"Hello", "World"}; Test t = new Test(); t.method(a); } void method(String...args) { System.out.println("Varargs"); // prints Varargs }