variadic-functions

Can we use va_arg with unions?

≡放荡痞女 提交于 2020-01-01 04:52:08
问题 6.7.2.1 paragraph 14 of my draft of the C99 standard has this to say about unions and pointers (emphasis, as always, added): The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at any time. A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit- field, then to the unit in which it resides), and vice versa. All well and good, that means that it is legal

Which method is called? (Integer… a) vs. (int a, int b)

眉间皱痕 提交于 2020-01-01 01:14:10
问题 I just found out about a very interesting Java trick: void method1(Integer... a){ } So you can give this method as many integers as you want. Now if I have a similar (overloaded) method like this: void method1(int a, int b){ } Which method runs when I execute the following line: method1(1, 2); Well, I could find that out very easily by just testing it out with different method instructions but when I think about the "rules" in "overloading" methods then I have to make sure that every

Am I missing something, or do varargs break Arrays.asList?

不问归期 提交于 2019-12-31 01:52:07
问题 private void activateRecords(long[] stuff) { ... api.activateRecords(Arrays.asList(specIdsToActivate)); } Shouldn't this call to Arrays.asList return a list of Long s? Instead it is returning a List<long[]> public static <T> List<T> asList(T... a) The method signature is consistent with the results, the varargs throws the entire array into the list. It's the same as new ArrayList(); list.add(myArray) And yes, I know it's meant to be used like this: Arrays.asList(T t1, T t2, T t3) I guess what

Java 7 overloading with varargs [duplicate]

心已入冬 提交于 2019-12-30 18:45:11
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: bug with varargs and overloading? could anyone explain me how this one works: class Vararg { static void vararg(int... x) { System.out.println("Integer..."); } static void vararg(long... x) { System.out.println("long..."); } public static void main(String [] args) { int s = 0; vararg(s,s); } } Get compile time error class Vararg { static void vararg(Integer... x) { System.out.println("Integer..."); } static void

java warning: Varargs method could cause heap pollution from non-reifiable varargs parameter

白昼怎懂夜的黑 提交于 2019-12-30 17:26:28
问题 I am using IntelliJ IDEA with javac on JDK 1.8. I have the following code: class Test<T extends Throwable> { @SafeVarargs final void varargsMethod( Collection<T>... varargs ) { arrayMethod( varargs ); } void arrayMethod( Collection<T>[] args ) { } } IntelliJ IDEA does not highlight anything in the above code as a warning. However, when compiling, the following line appears in the "Make" tab of the "Messages" view: Warning:(L, C) java: Varargs method could cause heap pollution from non

How to call a varargs method with an additional argument from a varargs method

ぃ、小莉子 提交于 2019-12-30 16:28:06
问题 I have some varargs system function, where T is some actual type, like String : sys(T... args) I want to create own function, which delegates to the system function. My function is also a varargs function. I want to pass through all the arguments for my function through to the system function, plus an additional trailing argument. Something like this: myfunc(T... args) { T myobj = new T(); sys(args, myobj); // <- of course, here error. } How do I need to change the line with the error? Now I

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

 ̄綄美尐妖づ 提交于 2019-12-30 10:19:29
问题 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, . . . ,

creating va_list dynamically in GCC - can it be done?

谁说胖子不能爱 提交于 2019-12-30 08:30:48
问题 my problem with vsprintf is that I can not obtain input arguments directly, I have to first get inputs one by one and save them in void** , then pass this void** to vsprintf() , it is all fine for windows, but when I come to 64bit linux, gcc cannot compile because it is not allowed to convert from void** to va_list , Is there anyone that can give me some help how I should do this under linux? Can I create va_list dynamically in GCC? void getInputArgs(char* str, char* format, ...) { va_list

How to find the length of a parameter pack?

百般思念 提交于 2019-12-30 06:35:27
问题 Suppose I have a variadic template function like template<typename... Args> unsigned length(Args... args); How do I find the length of the parameter list using the length function ? 回答1: Use sizeof... : template<typename... Args> constexpr std::size_t length(Args...) { return sizeof...(Args); } Note you shouldn't be using unsigned , but std::size_t (defined in <cstddef> ). Also, the function should be a constant expression. Without using sizeof... : namespace detail { template<typename T>

PHP variable length arguments?

流过昼夜 提交于 2019-12-30 06:19:10
问题 In Python and others, there's special syntax for variable length argument lists: def do_something(*args): # do something do_something(1, 2, 3, 4, 5, ...) # arbitrarily long list I was reading the PHP manual, and it said this: PHP 4 and above has support for variable-length argument lists in user-defined functions. This is really quite easy, using the func_num_args(), func_get_arg(), and func_get_args() functions. No special syntax is required, and argument lists may still be explicitly