variadic-functions

Verifying variable arguments are of expected type

隐身守侯 提交于 2019-12-09 17:56:33
问题 I'm currently writing a function which will take a variable number of arguments. I pass the number of arguments into the function and then will iterate through the arguments list. Each of the passed arguments should be an integer. I will be adding this integer to a vector of integers which will be used later. I would like to make sure that some joker doesn't attempt to pass this function something other then an integer in the future. I recognize that I can check the current argument from va

How to use “…” (variable) argument? [duplicate]

南楼画角 提交于 2019-12-09 13:44:27
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What are variadic functions in accordance with C and C++? I've seen ... argument in printf() function. Exactly HOW functions like printf or scanf work? How is it that they can have infinite input values? 回答1: This relies on the C calling convention, which is that the caller pops the arguments from the stack. Because it's the caller that knows how many arguments (and what size they are) it must communicate this

Standard way to manipulate variadic arguments?

家住魔仙堡 提交于 2019-12-09 11:10:40
问题 This is a weird question, but is there a standard way to manipulate the contents of a va_list before passing it to another function? For instance, suppose I have two functions, sum and vsum : int vsum(int n, va_list ap) { int total = 0; for (int i = 0; i < n; ++i) { total += va_arg(n, int); return total; } int sum(int n, ...) { va_list ap; va_start(ap, n); int total = vsum(n, ap); va_end(ap); return total; } If I call sum as sum(4, 1, 2, 3, 4) , I expect to get the result 10. Now let's

marshaling variable arguments - __arglist or alternative

喜欢而已 提交于 2019-12-09 03:44:25
Best way to describe the problem I'm trying to solve is to talk in code. I see a lot of __arglist questions on this forum, but not a lot of helpful answers. I know _arglist should be avoided so I'm open to alternative methods In one C++ module I have something like the following void SomeFunction(LPCWSTR pszFormat, va_args args) { // this function is not exported... // it is designed to take a format specifier and a list of variable // arguments and "printf" it into a buffer. This code // allocates buffer and uses _vsnwprintf_s to format the // string. // I really do not have much flexibility

Method overloading with variable arguments (varargs) [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-09 02:21:46
问题 This question already has answers here : Varargs Java Ambiguous Call (2 answers) Closed 4 years ago . I am surprised by seeing the output of this code : public class File { public static void main(String[] args) { movie(); } static void movie(double... x) { System.out.println("No varargs"); } static void movie(int... x) { System.out.println("One argument"); } } It outputs, One argument Why is it so ? I thought that this code would not compile because the call to movie() is ambiguous , but it

Requiring at least one element in java variable argument list

假如想象 提交于 2019-12-09 02:09:01
问题 In this code construct: public MyClass(Integer... numbers) { do_something_with(numbers[]); } is it possible to require that numbers contains at least one entry in such a way, that this is checked at compile-time? (At run-time, of course, I can just check numbers.length.) Clearly I could do this: public MyClass(Integer number, Integer... more_numbers) { do_something_with(number, more_numbers[]); } but this isn't going to be very elegant. The reason I would like to do this is to make sure that

How to match empty arguments pack in variadic template

不打扰是莪最后的温柔 提交于 2019-12-08 20:33:33
问题 I have code: template<typename T> void loadBrush_sub_impl() { // do some work here } template<typename T, typename... Targs> void loadBrush_sub() { loadBrush_sub_impl<T>(); loadBrush_sub<Targs...>(); } template<> void loadBrush_sub<void>() { } // BasicBrush, BinaryBrush, SketchBrush, BasicEraser and MaskBased are class loadBrush_sub<BasicBrush, BinaryBrush, SketchBrush, BasicEraser, MaskBased, void>(); This is correct when compile it. However, I really want to drop the void in the call

How to write this polyvariadic composition function in Haskell?

不打扰是莪最后的温柔 提交于 2019-12-08 19:41:57
问题 Note: This is a repost of another question, which the author deleted. Here's the original question: I have this polyvariadic comp function in Javascript and was wondering if a similar implementation in Haskell were possible. I am mostly interested in comp 's type: const comp = f => Object.assign( g => comp([g].concat(f)), {run: x => f.reduce((acc, h) => h(acc), x)} ); const inc = n => n + 1; const sqr = n => n * n; const repeatStr = s => n => Array(n + 1).join(s); comp(repeatStr("*")) (inc)

Why can't @SafeVarags be applied to instance methods in a final class?

巧了我就是萌 提交于 2019-12-08 16:31:21
问题 According to the documentation of SafeVarargs, the @SafeVarargs annotation can be applied only to constructors or variable arity methods that are either static or final . This is, I have read, to eliminate issues with annotation inheritance; that is to say, annotations on methods are only allowed if the method cannot be overridden. Clearly, constructors, static methods, and final methods cannot be overridden. However, neither can private methods or methods in a final class . Someone has

Is @SafeVarargs an appropriate annotation for this method?

左心房为你撑大大i 提交于 2019-12-08 15:51:38
问题 I have a bit of Java code (using the Guava ImmutableList class): @Nonnull public static <E extends Event> UserHistory<E> forUser(long id, E... events) { List<E> list = ImmutableList.copyOf(events); return new BasicUserHistory<E>(id, list); } I am getting the usual heap pollution warnings that come with a method like this. Since my method is not doing any modifications of events , it cannot introduce a heap pollution. However, if (because of erasure) a client of this method calls it with a bad