以下是否导致明确定义的行为?也就是说,如果你将非vararg函数f作为vararg函数g转换并使用f期望的参数调用g,那么行为是否与使用这些参数调用f的行为相匹配?
class Base {};
class Derived1 : public Base {
public:
int getInt1() {return 1;}
};
class Derived2 : public Base {
public:
int getInt2() {return 2;}
};
typedef int (*vfunc)(...);
int foo (vfunc f) {
Derived1 d1;
Derived2 d2;
return f(&d1, &d2);
}
int bar (Derived1 * p1, Derived2 * p2) {
return p1->getInt1() + p2->getInt2();
}
int main (int argc, char ** argv) {
return foo((vfunc)bar); // Is this program guaranteed to return 3?
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
有没有什么方法可以让程序定义明确,即使使用专有关键字?比如做一些像__cdecl …
我想编写一个可变参数函数,它应该有1个参数,它不是string类型的可选参数,第二个字符串是可选的.我已经阅读了关于可变函数的语言规范,但考虑到D的许多选项,我想知道哪个是我问题的合适解决方案.
另外我应该如何使用强制转换和指针来复制指向的字符串void* _argptr(字符串在D中不可变的事实让我困惑).
编辑:我想要的是:
string foo (string nonopt, ...) { /*code*/ }
//...
string a = "x@a.t", b = "a.t";
auto s = foo(a);
auto s2 = foo(a, b);
Run Code Online (Sandbox Code Playgroud) 我有一个函数log_info(从printf实现中复制)接受变量号.参数并将其传递给vprintf:
int log_info(const char *format, ...) {
va_list arg;
int done;
va_start (arg, format);
done = vfprintf (stdout, format, arg);
va_end (arg);
return done;
}
Run Code Online (Sandbox Code Playgroud)
我想在这些参数前面添加和附加字符串.因此,如果用户以这种方式调用上述函数:
log_info("at iteration %d, float value is %f", i, f);
Run Code Online (Sandbox Code Playgroud)
而不是打印
at iteration 4, float value is 102.34
Run Code Online (Sandbox Code Playgroud)
我想要打印
[INFO] [at iteration 4, float value is 102.34] [timestamp: xxxx]
Run Code Online (Sandbox Code Playgroud)
我可以分3个步骤完成
fprintf(stdout, "[INFO] [");
vprintf(stdout, format, arg);
fprintf(stdout, "] [timestamp:%f]", ts);
Run Code Online (Sandbox Code Playgroud)
但是程序是多线程的,因此我希望所有数据都可以在一次调用vprintf中编写(vprintf是线程安全的).
另一种选择是锁定一个互斥锁,然后按照上面所示的3个步骤编写它,但是如果将字符串附加到args并不是太复杂,我想尝试一下.
编辑:由于使用互斥锁而导致的性能开销并不是真正的问题,但除非必要,否则我不想使用它.
提前致谢
我正在和varargs一起工作并且知道: -
public void myMethod(String... args, int val){}
Run Code Online (Sandbox Code Playgroud)
方法myMethod的变量参数类型String必须是最后一个参数.
如果两者都一样String,错误它的给定是相当的,但在这种情况下,我设置int为第二个参数,所以在运行时JVM可以检查参数的类型,可以区分如下: -
myMethod("HI", "HELLO", 9)
Run Code Online (Sandbox Code Playgroud)
这不可行吗?我遗漏的任何其他要点都会产生错误?
我在超类上有以下方法:
public void method(Example... examples) {
for (Example e : examples) {
e.doSomething();
}
}
Run Code Online (Sandbox Code Playgroud)
这是对子类的调用:
super.method(examples[0], examples[1], examples[2], examples[3], examples[4], examples[5], examples[6], examples[7], examples[8], examples[9], examples[10]);
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法来传递元素?类似于 super.method(examples[0 -> 10]) ?
我无法将整个数组 (super.method(examples)) 传递给该方法。
谢谢你。
我有一个简单的接口及其实现:
interface Iface {
fun doSomething(s: String)
}
class IfaceImpl : Iface {
override fun doSomething(s: String) {
println("Doing the job, s = $s")
}
}
Run Code Online (Sandbox Code Playgroud)
此外,还有两个相同的(至少我看不出区别)调用处理程序,一个在 Java 中,一个在 Kotlin 中:
public class JavaHandler implements InvocationHandler {
private final Iface target;
public JavaHandler(Iface target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Java handler works");
return method.invoke(target, args);
}
}
class KotlinHandler(private val target: Iface) : InvocationHandler {
override fun invoke(proxy: …Run Code Online (Sandbox Code Playgroud) 我有一个方法,它有一个 varargs 参数。它看起来像这样:
public void sampleFunction(String name, Object... args) {
}
Run Code Online (Sandbox Code Playgroud)
我想将 a 传递byte[]给此方法,但作为单个参数。我怎样才能做到这一点?
byte[] myByteArray = functionReturningAByteArray();
sampleFunction("Name", myByteArray);
Run Code Online (Sandbox Code Playgroud)
这会被视为一个数组,还是会myByteArray被视为一个单一的对象?
当我通过时byte[],IntelliJ 将其报告为Confusing primitive array argument to varargs method. 我试图按照它的建议转换myByteArray为 an Object,然后它报告它为多余的转换。
我想将它作为单个对象传递。
我想在 Racket 中定义一个带有未定义参数数量的函数,所以我使用省略号,但它不起作用:
(define (f x ...) (printf x ...))
(f "~a ~a" "foo" "bar")
Run Code Online (Sandbox Code Playgroud)
错误:
Arity mismatch
Run Code Online (Sandbox Code Playgroud)
如何用省略号在 Racket 中定义一个函数?
好的,所以我有一个类似的功能
public static UnorderedList newUnorderedList(Object... items) {
return new UnorderedList(
stream(items)
.peek(e -> checkNotNull(e, "Cannot create null list item"))
.map(e -> {
if (e instanceof Component) return newListItem((Component) e);
if (e instanceof String) return newListItem((String) e);
throw new IllegalArgumentException("List item must be String or Component but is neither: " + e.getClass().getName());
}).toArray(ListItem[]::new)
);
}
Run Code Online (Sandbox Code Playgroud)
(编辑:注意:UnorderedList这里是 Vaadin 对 html<ul>标记的实现,我不是要获取 java 列表。)
当您使用数组调用它时,这将触发警告,表示不清楚您是要将数组本身视为单个元素还是元素的容器。
不要立即看到解决此问题的优雅方法。我不喜欢这些:
Object[]Object...成Collection<Object>是否有注释或某些东西可以让编译器知道始终将数组解析为对带注释的方法的 vararg 调用?(在方法声明上,而不是调用站点上。)
我想在可变参数函数中调用可变参数函数。代码没有给出错误,但结果值不正确。这样做的正确方法是什么?
#include <stdarg.h>
void SecondFunction (int count, ...) {
va_list args;
va_start(args, count);
AddValues(count, args);
va_end(args);
};
void AddValues(int count, ...) {
va_list args;
va_start(args, count);
for(int i=count; i--; )
processItem(va_arg(args, void*));
va_end(args);
}
Run Code Online (Sandbox Code Playgroud)