我有以下界面:
interface Foo {
void bar(String a, int b);
}
Run Code Online (Sandbox Code Playgroud)
我想Foo.bar反思地调用(在Foo的实现上).但是,参数是在数组中,我不知道它的大小.
以下不起作用:
void gee(Foo someFoo, Method bar, Object[] args) {
bar.invoke(someFoo, args);
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为args编译器将其作为单个参数进行威胁,并且数组不会"扩展"为vararg,而是在一个包含单个元素的数组中(内部)包装,即
@Test
public void varArgTest() {
assertTrue(varArgFoo(new Object[] {1, 2}) == 1);
}
private static <T> int varArgFoo(T... arg) {
return arg.length;
}
Run Code Online (Sandbox Code Playgroud)
我如何Method.invoke()在这种情况下调用,以便将数组作为vararg进行威胁?
或者更一般的问题:当参数在数组中时我如何调用vararg方法我不知道数组的大小直到运行时.
这是一个例子:
package com.demo;
public class PassArray {
static void vaTest(int... v){
System.out.println("no of args : "+v.length+"contents: ");
for (int x:v){
System.out.println(x+" ");
}
}
static void vaTest(boolean... v){
System.out.println("no of args : "+v.length+"contents: ");
for (boolean x:v){
System.out.println(x+" ");
}
}
public static void main(String args[]){
vaTest(1,2,3);
vaTest(true,false,true);
vaTest();//Error:Ambiguous!
}
}
Run Code Online (Sandbox Code Playgroud)
谁能告诉我:
我有一些问题
1.为什么会出现模棱两可的错误?
2.i有一个Varargs参数就好
int doIt(int a,int b,int c,int... vals)
Run Code Online (Sandbox Code Playgroud)
为什么必须最后声明varargs?
我想在运行时计算lambda n参数n.下一个最佳解决方案是:
lambda *x : do_something_with_a_tuple( x )
Run Code Online (Sandbox Code Playgroud)
这几乎没问题,但是我想要确切的数字由Python本身检查并通过func_code查看.当它n是2时,它应该完全像:
lambda x1, x2 : do_something_with_a_tuple( (x1, x2) )
Run Code Online (Sandbox Code Playgroud)
随着n等于3:
lambda x1, x2, x3 : do_something_with_a_tuple( (x1, x2, x3) )
Run Code Online (Sandbox Code Playgroud)
所以我希望variadic函数表现得像n-adic.我可以在没有eval元编程的情况下这样做吗?
如何使用反射调用可变参数方法?像这个:
public static void ArgsMethod(__arglist)
{
ArgIterator ai = new ArgIterator(__arglist);
while(ai.GetRemainingCount() > 0)
{
Console.WriteLine(TypedReference.ToObject(ai.GetNextArg()));
}
}
Run Code Online (Sandbox Code Playgroud)
typeof(Program).GetMethod("ArgsMethod").Invoke(null,new object[0])抛出NotSupportedException。
#include <stdio.h>
int main(void)
{
char s[32];
example_1:
scanf("%s", s);
printf("%s\n", s);
example_2:
scanf("%s", &s[0]);
printf("%s\n", s);
example_3:
scanf("%s", &s);
printf("%s\n", s);
}
Run Code Online (Sandbox Code Playgroud)
为什么#3与其他2的工作方式相同?
#3有效吗?为什么?
我正在将 32 位应用程序转换为 64 位应用程序,我遇到的痛点之一是可变参数函数,它期望很长但可能传递一个整数,例如参数被硬编码为 -1 而不是 -1L源于 64 位长尺寸更改为 64 位。以这个示例代码为例:
#include <stdio.h>
#include <stdarg.h>
long varargsExample(int input, ...);
int main(int argc, char **argv)
{
varargsExample(5,
"TestInt", 0,
/* This will fail if read as a long */
"TestIntNegative", -1,
"TestLong", 0L,
"TestLongNegative", -1L,
NULL);
}
long varargsExample(int firstArg, ...)
{
va_list args;
char * name;
long nextValue;
va_start(args, firstArg);
while ((name = va_arg(args, char *)) != 0)
{
/* If the type is changed to read in an …Run Code Online (Sandbox Code Playgroud) 我正在用这种方式在C++中编写一个带有可变数量的参数(和不同类型)的函数
template<typename ...Ts>
void myFunction(Ts ...args)
{
//create std::tuple to access and manipulate single elements of the pack
auto myTuple = std::make_tuple(args...);
//do stuff
return;
}
Run Code Online (Sandbox Code Playgroud)
我想做什么,但我不知道怎样,是从元组推送和弹出元素,特别是第一个元素...类似的东西
//remove the first element of the tuple thereby decreasing its size by one
myTuple.pop_front()
//add addThis as the first element of the tuple thereby increasing its size by one
myTuple.push_front(addThis)
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我正在尝试 push_back 可变参数函数的参数,如下所示,但编译器说存在类型不匹配(由于参数是通用类型,而向量是 int)。我应该怎么做才能使参数兼容?
vector<int> x;
template<typename... Rest>
void foo(Rest... rest) {
x.push_back(rest...);
}
Run Code Online (Sandbox Code Playgroud) 如果我定义了一个可变函数:
#include <stdio.h>
#include <stdarg.h>
int f(char*s,...)
{
va_list ap;
int i=0;
va_start(ap, s);
while(s)
{
printf("%s ", s);
i++;
s=va_arg(ap,char*);
}
va_end(ap);
return i;
}
int main()
{
return f("a","b",0);
}
Run Code Online (Sandbox Code Playgroud)
gcc(linux x64)编译这个,exe运行并打印"ab".
有没有需要像以下一样的演员:
return f("a","b",(char*)0)
Run Code Online (Sandbox Code Playgroud)
在共同的系统上?
在 C11 中,我可以创建一个原型如下所示的函数:
void myVaFunc(const char* const conv, ...);
Run Code Online (Sandbox Code Playgroud)
我可以这样运行它:
myVaFunc("ici", 1, "test", 2);
Run Code Online (Sandbox Code Playgroud)
该函数会知道(在解析第一个参数之后)有 3 个附加参数(4 个带有初始参数),因此类型为int、string(字符指针)和int. 简单,但不是很优雅。最近我了解了_Generic关键字,它允许在编译时派生变量的类型。我开始想知道是否有一种方法可以将可变参数功能(不再是功能,因为它总是需要第一个静态参数)和_Generic功能相结合。为什么?为了删除第一个参数,它告诉函数如何解析其他参数。可以这样调用的宏存在吗?
MYVAFUNC(1, "test", 2);
Run Code Online (Sandbox Code Playgroud)
并以与前面所述相同的方式工作myVaFunc?
我现在考虑了一段时间,但无法弄清楚这是否可能。