variadic-functions

Comma omitted in variadic function declaration in C++

青春壹個敷衍的年華 提交于 2019-12-20 10:58:40
问题 I am used to declaring variadic functions like this: int f(int n, ...); When reading The C++ Programming Language I found that the declarations in the book omit the comma: int f(int n...); // the comma has been omitted It seems like this syntax is C++ specific as I get this error when I try to compile it using a C compiler: test.c:1:12: error: expected ‘;’, ‘,’ or ‘)’ before ‘...’ token int f(int n...); Is there any difference between writing int f(int n, ...) and int f(int n... )? Why was

Java 3 dots parameter (varargs) behavior when passed no arguments or null

本秂侑毒 提交于 2019-12-20 10:14:52
问题 I tried this and get weird behavior from JAVA, can someone explain this for me? boolean testNull(String... string) { if(string == null) { return true; } else { System.out.println(string.getClass()); return false; } } boolean callTestNull(String s) { return testNull(s); } Then I have test case: @Test public void test_cases() { assertTrue(instance.testNull(null)); // NULL assertFalse(instance.testNull()); // NOT NULL assertFalse(instance.callTestNull(null)); // NOT NULL } The question is if I

Function prototype with ellipsis [duplicate]

笑着哭i 提交于 2019-12-20 07:48:19
问题 This question already has answers here : what is the use of … in c++ (2 answers) Closed 5 years ago . I was wondering if the below function prototype is valid. It compiled fine, but the three period is kinda throwing me off and I couldn't find anything similar on Google. void foo(int, ...); Thanks! 回答1: Yes, it's valid. In this example, ... creates a variadic function using the va_list mechanism. This is how variadic functions are implemented in C, and to some degree in C++ (though C++11's

Variadic functions and constants

别说谁变了你拦得住时间么 提交于 2019-12-20 04:03:05
问题 How exactly do variadic functions treat numeric constants? e.g. consider the following code: myfunc(5, 0, 1, 2, 3, 4); The function looks like this: void myfunc(int count, ...) { } Now, in order to iterate over the single arguments with va_arg , I need to know their sizes, e.g. int , short , char , float , etc. But what size should I assume for numeric constants like I use in the code above? Tests have shown that just assuming int for them seems to work fine so the compiler seems to push them

save variable argument list for fprintf calls

五迷三道 提交于 2019-12-20 03:34:10
问题 I am writing a heavy multi threaded [>170 threads] c++11 program. Each thread is logging information into one file used by all threads. For performance reasons I want to create a log thread which is writing the information via fprintf() into the global file. I have no idea how to organize the structure into which the worker threads are writing the information which can be then read by the log thread. Why do I not call sprintf() in each worker thread and then just provide the output buffer to

ClassCastException while using varargs and generics

空扰寡人 提交于 2019-12-19 21:19:53
问题 I'm using java generics and varargs. If I use the following code, I'll get a ClassCastException , even though I'm not using casts at all. Stranger yet, if I run this on Android (dalvik) no stack trace is included with the exception, and if I change the interface to abstract class, the exception variable e is empty. The code: public class GenericsTest { public class Task<T> { public void doStuff(T param, Callback<T> callback) { // This gets called, param is String "importantStuff" // Working

Warning for generic varargs

我只是一个虾纸丫 提交于 2019-12-19 17:32:32
问题 I have declared the following method: private void mockInvokeDBHandler(Map<String, Object>... rows) { List<Map<String, Object>> allRows = Arrays.asList(rows)); // rest of method omitted } It is invoked by clients using something like Map<String, Object> row1 = new HashMap<String, Object>(); Map<String, Object> row2 = new HashMap<String, Object>(); mockInvokeDBHandler(row1, row2); However, the last line shown above generates a warning Type safety : A generic array of Map is created for a

Warning for generic varargs

家住魔仙堡 提交于 2019-12-19 17:32:15
问题 I have declared the following method: private void mockInvokeDBHandler(Map<String, Object>... rows) { List<Map<String, Object>> allRows = Arrays.asList(rows)); // rest of method omitted } It is invoked by clients using something like Map<String, Object> row1 = new HashMap<String, Object>(); Map<String, Object> row2 = new HashMap<String, Object>(); mockInvokeDBHandler(row1, row2); However, the last line shown above generates a warning Type safety : A generic array of Map is created for a

Using `void_t` to detect multiple inheritance type repetition errors

这一生的挚爱 提交于 2019-12-19 05:54:37
问题 I want to implement a has_no_duplicates<...> type trait that evaluates to std::true_type if the passed variadic type list has no duplicate types. static_assert(has_no_duplicates<int, float>{}, ""); static_assert(!has_no_duplicates<float, float>{}, ""); Let's assume, for the scope of this question, that I want to do that using multiple inheritance. When a class inherits from the same type more than once, an error occurs. template<class T> struct type { }; template<class... Ts> struct dup

Using varargs in a Tag Library Descriptor

血红的双手。 提交于 2019-12-19 05:33:28
问题 Is it possible to have a TLD map to the following function: public static <T> T[] toArray(T... stuff) { return stuff; } So that I can do: <c:forEach items="${my:toArray('a', 'b', 'c')}"... I tried the following <function-signature> s java.lang.Object toArray( java.lang.Object... ) java.lang.Object[] toArray( java.lang.Object[] ) And others but nothing seems to work. 回答1: Unfortunately that's not possible. The EL resolver immediately interprets the commas in the function as separate arguments