variadic-functions

Why ambiguous error when using varargs overloading with primitive type and wrapper class? [duplicate]

空扰寡人 提交于 2019-11-30 20:03:35
This question already has an answer here: Ambiguous varargs methods 4 answers I do not understand why here in case 1, it is not giving compilation error, contrary in case 2 (varargs), it gives compilation error. Can anyone please elaborate what differences the compiler makes in these two cases? I went through many posts about it, but not able to understand it yet. Case #1 public class Test { public void display(int a) { System.out.println("1"); } public void display(Integer a) { System.out.println("2"); } public static void main(String[] args) { new Test().display(0); } } The Output is: 1 Case

Nice way to force user fill varargs parameter in Java [duplicate]

会有一股神秘感。 提交于 2019-11-30 19:38:21
This question already has an answer here: Requiring at least one element in java variable argument list 7 answers I want to force the user to fill in an optional parameter when calling my constructor: public MyClass(String... params) { this.params = params; } Currently, the following code is valid: new MyClass(); I want to prevent it. I thought of this: public MyClass(String param1, String... otherParams) { this.params = new String[1 + otherParams.length]; this.params[0] = param1; // fill 1..N params from otherParams } String[] params is not an option, because I need the ability to use comma

Template template parameters with variadic templates

半世苍凉 提交于 2019-11-30 19:24:47
问题 For the sake of clarity, I've removed things like the constructor & destructor etc from the below where they don't add anything to the question. I have a base class that is used to create a common ancestor for a derived template class. class PeripheralSystemBase { public: virtual void someFunctionThatsCommonToAllPeripherals() {} }; template <class T, uint32_t numPeripherals = 1> class PeripheralSystem : public PeripheralSystemBase { public: PeripheralSystem() : vec(T) {} std::vector<T> vec; /

To invoke a variadic function with unamed arguments of another variadic function

╄→尐↘猪︶ㄣ 提交于 2019-11-30 18:54:57
I have two variadic function as foo(format, ...) and bar(format, ...) . I want to implement function foo so that it can invoke bar with the same list of arguments it has. That is, foo(format...) { ... bar(format, ...); } For instance, invoking foo("(ii)", 1, 2) will invoke bar with same arguments bar("(ii)", 1, 2) . How should this foo function be implemented? PS: function bar is from a legacy library which I cant change its interface. Can't be done, as long as all you have is a bunch if functions with ... arguments. You have to plan ahead for things like that and implement each variadic

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

流过昼夜 提交于 2019-11-30 16:49:01
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); } } 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", "am", "arguments"]); } ( Playground ) Beyond that, you can explicitly accept optional arguments: fn foo

Why won't Java pass int[] to vararg? [duplicate]

一个人想着一个人 提交于 2019-11-30 15:45:43
This question already has an answer here: cannot make a static reference to a non static method 5 answers Why won't this compile? public class PrimitiveVarArgs { public static void main(String[] args) { int[] ints = new int[]{1, 2, 3, 4, 5}; prints(ints); } void prints(int... ints) { for(int i : ints) System.out.println(i); } } It complains about line 5, saying: method prints in class PrimitiveVarArgs cannot be applied to given types; required: int[] found: int[] reason: varargs mismatch; int[] cannot be converted to int but as far as I ( and others on SO ) know, int... is the same as int[] .

Type trait to obtain default argument promotions

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 14:04:58
问题 [Disclaimer: I know an answer to this question. I thought it might be of some general interest.] Question: How can we have a type trait that produces the type that results from performing default argument promotions ? Motivation: I would like to be able to use variable arguments portably. For example: void foo(char const * fmt, ...); // Please pass: * unsigned short // * bool // * char32_t // * unsigned char When passing arguments to a function call without parameters, i.e. matching the

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

落爺英雄遲暮 提交于 2019-11-30 13:55:20
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 I'm translating has a dynamic piece of data in it. "Player Score:%d" That is the key string I need to

Simplified Varargs Method Invocation in Java 7

心不动则不痛 提交于 2019-11-30 13:15:29
问题 In Java 7, you have the option to put a @SafeVarargs annotation to suppress the warning you get when compiling a method with a non-reifiable varargs parameter. Project Coin's proposal stipulates that the annotation should be used when the method ensures that only elements of the same type as the varargs parameter are stored in the varargs array. What would be an example of a non-safe method? 回答1: For example, foo() is not safe, it may store non-T in the array, causing problem at [2] <T

binding/applying constructors in JavaScript

房东的猫 提交于 2019-11-30 12:50:20
I was looking for solutions for calling Javascript constructors with an arbitrary number of arguments, and found some good SO posts, which led me to believe that these three calls should work the same. However, at least in rhino and node.js, they do not: 1. f = Date.bind(Date, 2000,0,1) 2. g = Date.bind.call(Date, 2000, 0, 1) 3. h = Date.bind.apply(Date, [2000, 0, 1]) The first one has the desired result: print(new f()) //=> Sat Jan 01 2000 00:00:00 GMT-0500 (EST) But the other two don't: print(new g()) //=> Thu Feb 01 1900 00:00:00 GMT-0500 (EST) print(new h()) //=> Wed Jun 01 1904 00:00:00