variadic-functions

Java 1.7 varargs function reported as unchecked warning

喜夏-厌秋 提交于 2019-12-01 15:03:33
We use some varargs functions and as we move to java 1.7 we are getting a strange unchecked warning. Function add in interface ICache public interface ICache<O> { void add(Object source, O... objects); } in an interface reports the error. ICache.java:18: warning: [unchecked] Possible heap pollution from parameterized vararg type O void add(Object source, O... objects); where O is a type-variable: O extends Object declared in interface ICache 1 warning O extends Object, as its generic cache class. I read the xlint warnings and we do compile with unchecked on, but http://docs.oracle.com/javase/7

Alternatives to function overloading in C

拜拜、爱过 提交于 2019-12-01 14:31:47
I'm looking for an elegant way to avoid re-writing a function, whose implementation is almost the same, but only the signature (the number of input parameters and their data types) is different. I know function overloading is not possible in C. I also know about the existence of variadic functions. But I think they won't be helpful in this situation. Consider the following problem, where we need to calculate the area of a triangle. We have two functions implementing two different formulae: S = 1/2bh and S = sqrt(s(s-a)(s-b)(s-c)). Apart from calculating the area each of the functions also

Arrays.asList() Confusing source code

ぐ巨炮叔叔 提交于 2019-12-01 14:26:22
问题 According to this source code for the Arrays class, the method asList passes an array to the constructor of new ArrayList . But there is no such constructor. Doesn't varargs generate an array, so how is this possible? Here is the asList source: public static <T> List<T> asList(T... a) { return new ArrayList<T>(a); } 回答1: java.util.Arrays.ArrayList is a different class than java.util.ArrayList. 来源: https://stackoverflow.com/questions/12015980/arrays-aslist-confusing-source-code

Java 1.7 varargs function reported as unchecked warning

十年热恋 提交于 2019-12-01 13:54:12
问题 We use some varargs functions and as we move to java 1.7 we are getting a strange unchecked warning. Function add in interface ICache public interface ICache<O> { void add(Object source, O... objects); } in an interface reports the error. ICache.java:18: warning: [unchecked] Possible heap pollution from parameterized vararg type O void add(Object source, O... objects); where O is a type-variable: O extends Object declared in interface ICache 1 warning O extends Object, as its generic cache

Inlining of vararg functions

时光怂恿深爱的人放手 提交于 2019-12-01 13:52:26
问题 While playing about with optimisation settings, I noticed an interesting phenomenon: functions taking a variable number of arguments ( ... ) never seemed to get inlined. (Obviously this behavior is compiler-specific, but I've tested on a couple of different systems.) For example, compiling the following small programm: #include <stdarg.h> #include <stdio.h> static inline void test(const char *format, ...) { va_list ap; va_start(ap, format); vprintf(format, ap); va_end(ap); } int main() { test

Variadic function without specified first parameter?

筅森魡賤 提交于 2019-12-01 08:53:16
问题 Out of curiosity, I thought I'd try and write a basic C++ class that mimics C#'s multiple delegate pattern. The code below mostly does the job, with the nasty sacrifice of losing almost all type-safety, but having to use the initial dummy parameter to set up the va_list really seems a bit off. Is there a way to use va_list without this? I do realize there are ways to do this with (for example) boost, but I was aiming for something dead simple that used just the standard library. #include

Calling a variadic function with an unknown number of parameters

爱⌒轻易说出口 提交于 2019-12-01 08:41:35
Say I have a function that takes a variable number of parameters: I want to call this function from somewhere else, building the list of parameters, but without knowing in advance how many parameters I'll need. Sorry that's not well explained, hopefully this code makes my question clearer: void foo(int n, ...) { va_list vl; va_start(vl,n); for (int i = 0; i<n; i++) { // Do something to each passed variable } } That function's being called from this one: void bar(int howManyParams) { // Here I want to call foo() with howManyParams parameters // (values are irrelevant for the question) // // I.e

Unpacking slice of slices

让人想犯罪 __ 提交于 2019-12-01 08:33:19
I am curious about unpacking a slice of slices and sending them as arguments to a variadic function. Let's say we have a function with variadic parameters: func unpack(args ...interface{}) If we wan't to pass in a slice of interfaces it works, it doesn't matter if we unpack it or not: slice := []interface{}{1,2,3} unpack(slice) // works unpack(slice...) // works It gets tricky if we have a slice of slices. Here the compiler doesn't let us pass in an unpacked version: sliceOfSlices := [][]interface{}{ []interface{}{1,2}, []interface{}{101,102}, } unpack(sliceOfSlices) // works unpack

Java: compile-time resolution and “most specific method” as it applies to variable arity

旧街凉风 提交于 2019-12-01 08:22:40
Could someone help me understand section 15.12.2.5 of the JLS re: most specific method ? (bludgeoned cut&paste from JLS follows) In addition, one variable arity member method named m is more specific than another variable arity member method of the same name if either: One member method has n parameters and the other has k parameters, where n >= k. The types of the parameters of the first member method are T1, . . . , Tn-1 , Tn[], the types of the parameters of the other method are U1, . . . , Uk-1, Uk[]. If the second method is generic then let R1 ... Rp p1, be its formal type parameters, let

Calling a variadic function with an unknown number of parameters

蹲街弑〆低调 提交于 2019-12-01 06:40:34
问题 Say I have a function that takes a variable number of parameters: I want to call this function from somewhere else, building the list of parameters, but without knowing in advance how many parameters I'll need. Sorry that's not well explained, hopefully this code makes my question clearer: void foo(int n, ...) { va_list vl; va_start(vl,n); for (int i = 0; i<n; i++) { // Do something to each passed variable } } That function's being called from this one: void bar(int howManyParams) { // Here I