variadic-functions

Kotlin: Argument Type Mismatch when passing Array as vararg parameter

浪子不回头ぞ 提交于 2019-12-11 08:02:05
问题 I have a simple interface and its implementation: interface Iface { fun doSomething(s: String) } class IfaceImpl : Iface { override fun doSomething(s: String) { println("Doing the job, s = $s") } } Also, there are two identical (at least I cannot spot the difference) invocation handlers, one in Java and one in Kotlin: public class JavaHandler implements InvocationHandler { private final Iface target; public JavaHandler(Iface target) { this.target = target; } @Override public Object invoke

Cast specific types in variadic argument

时光总嘲笑我的痴心妄想 提交于 2019-12-11 07:35:11
问题 I have a template function that accepts variadic arguments. template<typename... Params> void foo(Params... p); I want to find all occurences of a given type ( const char* ) in Params to replace them with another type, that these values can be cast to (my own Path class with constructor Path(const char*) ). The idea is to have something like template<typename... Params> void foo(Params... p) { bar<convertCharPointerToPath<Params>...>(p...); } How can this conversion be done? 回答1: If you want

Variadic arguments, using Type Families instead of Multi-param Typeclasses

橙三吉。 提交于 2019-12-11 06:49:34
问题 Here's what I've got, expressed with MultiParamTypeClasses: class ListResultMult r a where lstM :: a -> [a] -> r listM :: ListResultMult r a => a -> r listM a = lstM a [] instance ListResultMult r a => ListResultMult (a -> r) a where lstM a as x = lstM x $ a:as instance ListResultMult [a] a where lstM a as = reverse $ a:as instance Show a => ListResultMult (IO ()) a where lstM a as = print . reverse $ a:as Here's what I tried, using TypeFamilies (TypeSynonymInstances didn't help): class

Should I cast a CString passed to Format/printf (and varargs in general)?

心不动则不痛 提交于 2019-12-11 06:36:36
问题 I recently took in a small MCF C++ application, which is obviously in a working state. To get started I'm running PC-Lint over the code, and lint is complaining that CStringT's are being passed to Format. Opinion on the internet seems to be divided. Some say that CSting is designed to handle this use case without error, but others (and an MSDN article) say that it should always be cast when passed to a variable argument function. Can Stackoverflow come to any consensus on the issue? 回答1:

Calling C functions with too many arguments

邮差的信 提交于 2019-12-11 06:16:18
问题 I am currently changing the function signatures of a class of functions in an application. These functions are being stored in a function table, so I was expecting to change this function table as well. I have just realised that in certain instances, we already use the new function signature. But because everything is casted to the correct function type as it is put into the function table, no warnings are being raised. When the function is called, it will be passed extra parameters that are

Java: Function with one varargs argument and function with same name and one argument of same type is allowed? [duplicate]

穿精又带淫゛_ 提交于 2019-12-11 05:58:41
问题 This question already has answers here : Why does type-promotion take precedence over varargs for overloaded methods (2 answers) Closed last year . While preparing for Java certification exam I was quite surprised to see that Java allows this: public class Consumer { public void buy(Object o) { System.out.println("Buying one object"); } public void buy(Object... o) { System.out.println("Buying multiple objects"); } public static void main(String[] args) { Consumer consumer = new Consumer();

calling java varargs from scala with overloading

时光怂恿深爱的人放手 提交于 2019-12-11 05:14:29
问题 It's another question about scala-java compatibility related to varargs feature. The key difference is that java's part is overloaded. It resembles this scala code: object Test { def test( xa : String* ) = print( xa.mkString(",") ) def test( xs : Seq[String] ) = print( xs.mkString(",") ) } Scala unlike java mark such overloading as invalid error: double definition: def test(xa: String*): Unit at line 11 and def test(xs: Seq[String]): Unit at line 12 have same type after erasure: (xa: Seq)Unit

template variadic function: arbitrary number of classes with arbitrary number of constructor parameters

馋奶兔 提交于 2019-12-11 05:07:09
问题 I am unsure of what the proper name is, so far I comprehend the notion of using variadic template arguments (e.g., in comparison to std::initializer_list ). So let's assume there is an arbitrary number of classes k and an arbitrary number of parameters i which is dependant on each class k constructor. I know that I may construct any class k using a variadic template: template <typename T, typename... Args> void make_class(Args... args) { auto k = T(args...); } However I want to allow creation

Why are some library routines implemented as macros simultaneously? Why is “va_arg” macro declared as a function (without “#define”)?

泪湿孤枕 提交于 2019-12-11 05:05:50
问题 I am struggling to put it clearly in words. So let me put it in parts. The contexts are from the C book by Mike Banahan (Links provided with each part below). Here are my questions as bullet points in bold: Why are some library functions simultaneously implemented as macros too? What's the need? Here's what I read from the book (Section 9.1.1): A last general point is that many of the library routines may be implemented as macros, provided that there will be no problems to do with side

c++ variadic template pack expansion by groups of arbitrary size

邮差的信 提交于 2019-12-11 04:54:41
问题 (Somehow related to this previous question) I want to evaluate the parameters of a template function by groups of N parameters. Something like this: template <size_t N, typename ... Ts> void evaluate(Ts const & ... fn) { for( int i=0; i<2; i++ ) runH<N>::func(i, fn...); } int main() { run<3>( // N = 2 [](int i){ cout << "lambda func 1: " << std::to_string( i ) << endl; }, [](int i){ cout << "lambda func 2: " << std::to_string( i ) << endl; }, [](int i){ cout << "lambda func 3: " << std::to