variadic-functions

Kotlin function overloading (varargs vs single parameter)

和自甴很熟 提交于 2019-12-24 18:09:10
问题 I have two functions check if String/Strings are blank. fun isBlank(s: String?) : Boolean { return s.isNullOrBlank() } fun isBlank(vararg strings: String) : Boolean { return strings.isEmpty() || strings.any { isBlank(it) } } So I try to call first function from the second one but seems it tries to call itself. For instance it works nice in java: public static boolean isBlank(final String string) { return string == null || string.trim().isEmpty(); } public static boolean isBlank(final String..

Kotlin function overloading (varargs vs single parameter)

送分小仙女□ 提交于 2019-12-24 18:08:01
问题 I have two functions check if String/Strings are blank. fun isBlank(s: String?) : Boolean { return s.isNullOrBlank() } fun isBlank(vararg strings: String) : Boolean { return strings.isEmpty() || strings.any { isBlank(it) } } So I try to call first function from the second one but seems it tries to call itself. For instance it works nice in java: public static boolean isBlank(final String string) { return string == null || string.trim().isEmpty(); } public static boolean isBlank(final String..

wrapper for a function with variable arguments in C

孤人 提交于 2019-12-24 15:33:30
问题 Recently I wanted to implement a printf wrapper. After some search I found the vprintf is good for this need: void my_printf(const char *fmt, ...) { va_list args; va_start(args, fmt); vprintf(fmt, args); va_end(args); } But is it possible to implement such a wrapper for printf or any other similar functions with variable arguments instead of va_list ? (I mean, what if they don't provide a v version?) Since some commenter didn't fully capture my idea, I'd better elaborate it. Suppose you have

template dependent constructor argument lengths

主宰稳场 提交于 2019-12-24 13:54:07
问题 I try to make a generic, but still efficient multi dimension Point class. What I have is a Dimensions enum enum Dimension : std::size_t { _2D = 2, _3D = 3 }; And a Point class template <typename T, Dimension D> class Point : public std::array<T, D> { public: T& at(size_t idx) { return std::array<T,D>::at(idx); }; const T& at(size_t idx) const { return std::array<T,D>::at(idx); }; Dimension dim() const { return D; } ... }; I would like to create nices constructors so I added (outside my class

When overloading compiler does not prefer primitive to Object only in case of varargs parameter presence

坚强是说给别人听的谎言 提交于 2019-12-24 12:11:58
问题 Please, could you help me to understand why compilation of first call of testVargArgsAutoboxingPriority fails? In case of second call compiler is able to select proper method by preferring primitive (first parameter) to Object but after varargs parameter addition compiler is not able to make the selection any more. Fail message is \jdk1.6.0_45\bin\javac.exe ocjp6/AutoBoxingOldStyleVarargsPriority.java ocjp6\AutoBoxingOldStyleVarargsPriority.java:7: reference to testVargArgsAutoboxingPriority

Canonicalise args and kwargs to argument-canonical form

岁酱吖の 提交于 2019-12-24 10:55:46
问题 I'm looking for a way, given a function's signature, to canonicalize it's args and kwargs. That is, any kwargs passed in already in the signature of the function should be converted to args. For example: def myfunc(a, b=0, c=0, **kwargs): pass def canonicalize(func, *args, **kwargs): something = inspect.signature(func) # Do something with args/kwargs here return new_args, new_kwargs Example output: >>> canonicalize(myfunc, 1, 2, g=3) (1, 2), {'g': 3} >>> canonicalize(myfunc, 1) (1,), {} >>>

va_arg prevents me from calling a managed delegate in a native callback

左心房为你撑大大i 提交于 2019-12-24 10:39:11
问题 In a C++/CLI assembly, I'm trying to call a managed delegate from a native callback. I followed Doc Brown's answer here, and my implementation so far looks like this: The native callback - ignore the commented out parts for now: static ssize_t idaapi idb_callback(void* user_data, int notification_code, va_list va) { switch (notification_code) { case idb_event::byte_patched: { //ea_t address = va_arg(va, ea_t); //uint32 old_value = va_arg(va, uint32); return IdaEvents::BytePatched(0, 0); }

Variable argument pairs in MATLAB functions

人走茶凉 提交于 2019-12-24 07:59:18
问题 I'm trying to develop a function that contains multiple arguments. To be as robust as possible, I want to be able to call my function as follows: foo( x, y, z, 'OptionalArg1', bar, 'OptionalArg2', blah, 'OptionalArg3', val ) I want my function to be robust enough to contain any combination of these arguments in any order. I also need to be able to set defaults if the argument is not provided. Is there a standard way to do this in MATLAB? 回答1: The best way would be to use the inputParser class

variable number of arguments, same specific type without macro or initializer-list

时光怂恿深爱的人放手 提交于 2019-12-24 07:25:15
问题 I'm trying to do something similar to C++11 variable number of arguments, same specific type, but I have my own type: struct Foo { Foo(int) {} Foo(int, int) {} }; with a bunch of overloads void f() {} void f(const Foo&) {} void f(const Foo&, const Foo&) {} // etc. ... as many f() overloads as needed ... works as desired: f(); f(1); f(1, 2); f(1, { 2, 3 }); . Instead of overloads, I can also use std::initializer_list<> with the {} syntax (as suggested here): void g_(std::initializer_list<Foo>)

Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis

旧街凉风 提交于 2019-12-24 00:58:00
问题 I know that declaring a function (or function pointer) with no parameter list (and without specifying void in the parameter list) that means that the function (or function pointer) has an unknown number of arguments. I wrote some test scripts to check this behavior out: int my_func_1() { return(0); } int my_func_2(int x) { return(x); } int my_func_3(char x, ...) { va_list va; va_start(va, x); return(va_arg(va, int)); } int (*fp)(); fp = my_func_1; printf("%d\n", fp()); fp = my_func_2; printf(