variadic-functions

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

做~自己de王妃 提交于 2019-11-30 04:37:11
问题 This question already has answers here : Ambiguous varargs methods (4 answers) Closed 3 years ago . 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

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

爷,独闯天下 提交于 2019-11-30 03:48:59
问题 This question already has answers here : Requiring at least one element in java variable argument list (7 answers) Closed 4 years ago . 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]

(Im)perfect forwarding with variadic templates

时光毁灭记忆、已成空白 提交于 2019-11-30 03:39:14
Synopsis Given a type with a variadic template constructor that forwards the arguments to an implementation class, is it possible to restrict the types being forwarded with SFINAE? Details First, consider the non-variadic case with a constructor taking a universal reference. Here one can disable forwarding of a non-const lvalue reference via SFINAE to use the copy constructor instead. struct foo { foo() = default; foo(foo const&) { std::cout << "copy" << std::endl; } template < typename T, typename Dummy = typename std::enable_if< !std::is_same< T, typename std::add_lvalue_reference<foo>::type

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

前提是你 提交于 2019-11-30 03:02:43
问题 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. 回答1: Can't be done, as long as all you have is a bunch if

C++ Variadic Function Templates of Known Type

*爱你&永不变心* 提交于 2019-11-30 02:10:32
问题 I'm currently trying to get my head around some of the things I can do with variadic template support. Let's say I have a function like this - template <typename ... Args> void foo(Args ... a) { int len = sizeof...(tail); int vals[] = {a...}; /* Rest of function */ } /* Elsewhere */ foo(1, 2, 3, 4); This code works because I assume beforehand that the arguments will be integers, but obviously will fail if I provide something else. If I know that the parameter packs will contain a particular

Varargs to ArrayList problem in Java

穿精又带淫゛_ 提交于 2019-11-30 01:20:06
I don't understand why the following does not work: public void doSomething(int... args){ List<Integer> broken = new ArrayList<Integer>(Arrays.asList(args)) } Its my understanding that the compiler converts the "int... args" to an array, so the above code should work. Instead of working I get: cannot find symbol symbol: constructor ArrayList(java.util.List <int[] >) location: class java.util.ArrayList <java.lang.Integer > Thats bizarre. I'm not adding an array to array list, I'm adding each element from the list to the arraylist. Whats going on? Jonathan Java cannot autobox an array, only

Passing a List in as varargs [duplicate]

人盡茶涼 提交于 2019-11-29 23:29:15
This question already has an answer here: How to pass an ArrayList to a varargs method parameter? 4 answers I have a List<Thing> and I would like to pass it to a method declared doIt(final Thing... things) . Is there a way to do that? The code looks something like this: public doIt(final Thing... things) { // things get done here } List<Thing> things = /* initialized with all my things */; doIt(things); That code obviously doesn't work because doIt() takes Thing not List<Thing> . Is there a way to pass in a List as the varargs? This is in an Android App, but I don't see why the solution will

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

我是研究僧i 提交于 2019-11-29 22:50:46
问题 This question already has answers here : cannot make a static reference to a non static method (5 answers) Closed 3 years ago . 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:

binding/applying constructors in JavaScript

元气小坏坏 提交于 2019-11-29 18:22:39
问题 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:

BWDB SQLite wrapper for iOS ARC issues

拥有回忆 提交于 2019-11-29 17:40:03
I am trying to use Bill Weinman's BWDB wrapper, which can be downloaded here: http://bw.org/iosdata/ I can't convert it to ARC properly, can someone more experienced then i to look at it? Main issue is va_list in insertRow & updateRow methods, idk what to do with it. The rest of the errors are easy to get rid of. Thanks in advance for any help/advice! Header file // BWDB.h // Created by Bill Weinman on 2010-09-25. // Copyright 2010 The BearHeart Group, LLC. All rights reserved. #import <Foundation/Foundation.h> #import <sqlite3.h> #define defaultDatabaseFileName @"bwtest.db" #define BWDB