标签: variadic-functions

函数名前的python varargs?

我正在客户端代码库中进行一些Python编码,我偶然发现了一行看起来像这样的代码(变量名已被更改以保护无辜者):

reply = function1(a=foo, **function2(bar, b=baz))
Run Code Online (Sandbox Code Playgroud)

通常,参数列表中的**会收集剩余的关键字参数,但它们在函数名称前面做了什么?

python variadic-functions

2
推荐指数
1
解决办法
494
查看次数

如何创建函数并传入可变长度参数列表?

我们可以p在以下代码中创建一个函数:

var p = function() { };
if (typeof(console) != 'undefined' && console.log) {
    p = function() { console.log(arguments); };
}
Run Code Online (Sandbox Code Playgroud)

但是参数像数组一样传递给console.log,而不是一个接一个地传递

console.log(arguments[0], arguments[1], arguments[2], ... 
Run Code Online (Sandbox Code Playgroud)

有没有办法扩展参数并传递给console.log像上面的方式?

请注意,如果原始代码是

var p = function() { };
if (typeof(console) != 'undefined' && console.log) {
    p = console.log;
}
Run Code Online (Sandbox Code Playgroud)

然后它适用于Firefox和IE 8,但不适用于Chrome.

javascript lambda function variadic-functions

2
推荐指数
1
解决办法
1094
查看次数

va_arg返回错误的参数

使用以下代码,va_arg将通过vProcessType返回第二次和第三次传递的垃圾.

// va_list_test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <tchar.h>
#include <cstdarg>
#include <windows.h>

void processList(LPTSTR str, ...);
void vProcessList(LPTSTR str, va_list args);
void vProcessType(va_list args, int type);

int _tmain(int argc, _TCHAR* argv[])
{
    LPTSTR a = TEXT("foobar");
    int b = 1234;
    LPTSTR c = TEXT("hello world");
    processList(TEXT("foobar"), a, b, c);
    return 0;
}

void processList(LPTSTR str, ...)
{
    va_list args;
    va_start(args, str);
    vProcessList(str, args);
    va_end(args);
}
void vProcessList(LPTSTR str, va_list args)
{ …
Run Code Online (Sandbox Code Playgroud)

c c++ variadic-functions

2
推荐指数
1
解决办法
2329
查看次数

变异函数

#include <stdarg.h>
#include <stdio.h>

void varfun(int n,...){
va_list ptr;
int num;
va_start(ptr,n);
num=va_arg(ptr,int);
printf("\n%d",num);

}


int main(int argc, char **argv){

varfun(3,7.5,-11.2,0.66);

return 0;
}
Run Code Online (Sandbox Code Playgroud)

看看上面的代码,我希望输出是第一个输入到int的变量参数值,即7.5输入到int,即7.但输出为0.这有什么问题?

c variadic-functions

2
推荐指数
1
解决办法
6420
查看次数

传递字符串时,Java构造函数varargs发生冲突

我和我的一个班级有问题.我正在使用"varargs"构造函数来获取未知数量的参数.

public Groupe(String...nom){        
    for(String item:nom){   
        this.nom.add(item.toLowerCase());
    }
}

public Groupe(String nom){      
    String[] list =nom.split(",");
    for(String s : list){           
        this.nom.add(s.toLowerCase());
    }
}
Run Code Online (Sandbox Code Playgroud)

第一个构造函数被调用......这很好,但是当第二个构造函数只传递一个参数时会发生冲突.我只想在传递一个字符串时使用第二个构造函数,并且第一个if 2和更多参数.

我想要处理这个新的Groupe("Foo,Bar");

这就是我所说的.我怀疑"错误"来自那里

public void reserver(String...nom){
    Groupe gr = new Groupe(nom);
    passager.add(gr);       
}
Run Code Online (Sandbox Code Playgroud)

我不传递字符串,而是传递Varargs(tab?)...

java constructor conflict variadic-functions

2
推荐指数
1
解决办法
7650
查看次数

va_arg返回值警告的标准C类型

我正在开发一个C程序,并被这个警告所困扰.我想使用从列表中检索参数va_arg.

args[i] = (int) va_arg(argptr, int); 
Run Code Online (Sandbox Code Playgroud)

要么

args[i] = (char) va_arg(argptr, char);
Run Code Online (Sandbox Code Playgroud)

得到此警告的问题:

... void *' differs in levels of indirection from 'int'...

同样也适用于char案例.对此有何解释?

码:

void test_function(va_list argptr, int (*callback)(),
                   int ret_typel)
{
  int i ;
  int arg_typel;
  int no_moreb = TRUE;
  void *args[MAX_FUNCTION_ARGS];
  for (i=0; no_moreb; i++) {
    arg_typel = (int)va_arg(argptr, int);
    switch(arg_typel) {
    case F_INT:
      args[i] = (int) va_arg(argptr, int);
      break;
    case F_CHAR:
      args[i] = (char) va_arg(argptr, char);
      break;
    default:
      no_moreb = FALSE;
      i--;
      break;
    } …
Run Code Online (Sandbox Code Playgroud)

c variadic-functions

2
推荐指数
1
解决办法
3339
查看次数

获取getDeclaredMethods()以匹配varargs方法

我想用它getDeclaredMethod()来找到一个带有这个签名的方法:

public void foo(String inArg1, Object... inArgs);
Run Code Online (Sandbox Code Playgroud)

使用此电话:

Class<?>[] argClasses = { String.class, Integer.class };
Method m = clazz.getDeclaredMethod("foo", argClasses);
Run Code Online (Sandbox Code Playgroud)

但它产生了一个NoSuchMethodException例外.但是,可以调用该方法(假设您以其他方式找到它):

Object[] args = { "arg1", new Integer(2) };
m.invoke(instance, args);
Run Code Online (Sandbox Code Playgroud)

我可以将它们全部列出来,getDeclaredMethods()然后尝试自己进行签名匹配,但这似乎很多工作.

我应该做什么?我只是缺少一些愚蠢的东西吗?

java reflection variadic-functions

2
推荐指数
1
解决办法
3865
查看次数

将Seq [T]作为Scala中的vararg传递

此代码将无法编译

val sortedSet = SortedSet[Int](Array(1,2,3,4).toSeq)

  Error: type mismatch; found  :Seq[Int] required Int
Run Code Online (Sandbox Code Playgroud)

但是,以下是SortedSet中apply的定义:

def apply[A](elems: A*)(implicit ord: Ordering[A]): CC[A] = (newBuilder[A](ord) ++= elems).result
Run Code Online (Sandbox Code Playgroud)

它说elem是一个vararg因此应该是Seq [A]类型我错过了什么?为什么我不能通过seq作为vararg?

scala sequence variadic-functions

2
推荐指数
1
解决办法
426
查看次数

C中的变量函数f()与f(...)

以下两个可变函数定义有什么区别?

int f()
{
    /* function definition */
}

int f(...)
{
    /* function definition */
}
Run Code Online (Sandbox Code Playgroud)

f()实际上定义为可变函数.我也假设<stdarg.h>可以包含和使用.

c variadic-functions

2
推荐指数
1
解决办法
88
查看次数

printf%f如何在32位浮点数上运行

%fprintf格式代码被指定为上式的值操作double[ .但是,一个简单的测试程序表明它也可以与类型的值一起使用float.这是如何运作的?

具有整数类型(例如intlong long int)的等效情况"起作用",因为在小端机器上,32位整数的低位字节恰好与64位整数的低位字节重叠,所以只要高位为0,你会得到"正确"的答案.

但是,这不可能是对的情况下floatdouble,因为浮点格式都没有这样的互换.如果没有将(相当复杂的)转换为其他格式,您根本无法将浮点值打印为double.通过类型惩罚尝试这样做只会打印垃圾.

最重要的是,printf是可变的.编译器在编译时不一定知道将使用什么格式说明符,只知道参数的类型.因此,我唯一可以推测的是,传递给可变函数的所有 float值都将double无条件地升级到.但令我难以置信的是,我可以用C语言编程很长时间而且不知道这一点.

C如何在这里进行隐式强制?

资源:

#include <stdio.h>
#include <math.h>

int main() {
  float x[2] = {M_PI, 0.0};
  printf("value of x: %.16e\n", x[0]);
  printf("size of x: %lu\n", sizeof(x[0]));

  double *xp = (double *)&x[0];
  printf("value of *xp: %.16e\n", *xp);
  printf("size of *xp: %lu\n", sizeof(*xp));

  double y = M_PI;
  printf("value of y: %.16e\n", y);
  printf("size of y: …
Run Code Online (Sandbox Code Playgroud)

c printf variadic-functions

2
推荐指数
2
解决办法
192
查看次数