标签: variadic-functions

Var arg list in main

我想使用我的程序:

./program -I /usr/include/ /usr/bin/ /usr/local/include/ ...
Run Code Online (Sandbox Code Playgroud)

交换机可以像var args列表一样继续运行.我怎么能在C99那样做?最好得到一个类似char **args_listchar *args_list[]包含所有喜欢的东西/usr/include/usr/bin/.

c c99 variadic-functions

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

如何通过C90中的函数传递va_list

我想将va_list传递给另一个函数.这是我想要做的一个例子:

void my_printf_1(char* string, ...) {
    va_list ap;
    va_start(ap, string);
    printf(string, ap);
    va_end(ap);
}

void my_printf_2(char* string, ...) {
    va_list ap;
    va_start(ap, string);
    printf(string, *ap);
    va_end(ap);
}

int main(void) {
    my_printf_1("Hello %i\n", 12); //Shows me the pointer
    my_printf_1("Hello \n"); //Runtime Error - too many arguments
    my_printf_2("Hello %i\n", 12); //Displays correctly because 12 < char
    my_printf_2("Hello %i\n", 500); //Displays incorrectly  because 500 > char
    my_printf_2("Hello %i %i\n", 12, 12); //Displays 12, then crashes Runtime Error not enough arguments
    my_printf_2("Hello \n"); //Runtime …
Run Code Online (Sandbox Code Playgroud)

c variadic-functions cvi c89

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

重载构造函数中的重复参数

我想在Scala中编写一个类,它采用任意数量的字节或像这样的布尔值

class Bytes(data: Byte*) {
  def this(data: Boolean*) = this {
    val res: Array[Byte] = convBools2Bytes(data)

    res: _*
  }

  // […]
}
Run Code Online (Sandbox Code Playgroud)

其中convBools2Bytes是一个转换函数Array[Boolean]Array[Byte]:

def convBools2Bytes(data: Array[Boolean]): Array[Byte]
Run Code Online (Sandbox Code Playgroud)

这给了我以下编译器错误:

[error] Bytes.scala:5: no `: _*' annotation allowed here
[error] (such annotations are only allowed in arguments to *-parameters)
[error]     res: _*
[error]        ^
Run Code Online (Sandbox Code Playgroud)

据我了解,该res: _*语句将Array[Byte]转换为重复参数列表(如"Scala编程"第2章第8节中所述).

为什么会出现这样的错误,我该如何避免呢?

constructor scala variadic-functions

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

为什么输出int?

为什么主方法不调用第一种方法?

public class Test {           

    public static void printValue(byte...b) {  
        System.out.println("long");  
    }  

    public static void printValue(int i, int j, int k) {  
         System.out.println("int");  
    }

    public static void main(String... args) {  
        byte b = 9;  
        printValue(b,b,b);  
    }  
}  
Run Code Online (Sandbox Code Playgroud)

为什么输出int

java overloading variadic-functions

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

变量函数 - 确定返回类型

我正在使用可变参数模板,我坚持以下内容:

template <class T1, class T2>
auto sum(T1 a, T2 b) ->decltype(a + b){
    return a + b;
}
template <class T1, class T2, class... T3>
auto sum(T1 a, T2 b, T3... tail) ->decltype(a + b){
    return a + sum(b, tail...);
}
Run Code Online (Sandbox Code Playgroud)

函数调用:

cout << sum(1, 2, 3, 4) << endl;    // 10 - OK
cout << sum(1.5, 2, 3, 4) << endl;  // 10.5 - OK
cout << sum(1, 2, 3.5, 4) << endl;  // 10 !! wrong result …
Run Code Online (Sandbox Code Playgroud)

c++ variadic-functions c++11

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

为什么使用varargs的重载方法导致StackOverflowError?

我实现了一个带有重载方法的Scala类,该方法可以带一个Iterable[String]或一个String*varargs参数:

class StackOverflow(names: Iterable[String]) {

  // This function creates a copy of the StackOverflow object
  // copy is needed but this cannot be a case class.
  private def copy(names: Iterable[String] = names) = new StackOverflow(names) // <- line 19

  // overloaded methods

  def withNames(names: Iterable[String]) = this.copy(names = names) // <- line 24

  def withNames(names: String*) = require(names.nonEmpty); withNames(names.toIterable) // <- line 26
}

object App {

  def main(args: Array[String]) = {
    val x1 = new …
Run Code Online (Sandbox Code Playgroud)

scala variadic-functions

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

默认参数提升和printf中“%c”的相关性

libc关于变参函数,这是要说的:

由于原型没有为可选参数指定类型,因此在对可变参数函数的调用中,默认参数提升是对可选参数值执行的。这意味着将char或short int类型的对象(无论是否带符号)提升为int或unsigned int;并且将float类型的对象提升为double类型。因此,如果调用方将char作为可选参数传递,它将被提升为int

那么,为什么会有人使用"%c",或"%hd"在printf的?他们应该只使用"%d"

我还看到没有的格式说明符floatfloat必须与之%f共存,double因为由于晋升,不可能接受浮夸作为可变参数。

我知道scanf,参数是指针,并且没有提升。

我是否有任何理由想念为什么以及何时"%c"必须存在printfs?

c printf variadic-functions

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

关于模板替换示例的混淆

我对"The C++ programming language 4th,28.4.4"中的代码感到困惑

template<typename T>
struct get_f_result {
private:
    template<typename X>
    static auto check(X const& x) ?> decltype(f(x)); // can call f(x)
    static substitution_failure check(...); // cannot call f(x)
public:
    using type = decltype(check(std::declval<T>()));
};
Run Code Online (Sandbox Code Playgroud)

我特别困惑的部分是这一行:

static substitution_failure check(...); // cannot call f(x)
Run Code Online (Sandbox Code Playgroud)

但我记得...不能接受非豆荚类型?那怎么可能呢?

c++ variadic-functions sfinae c++11

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

PHP致命错误:参数解压缩后无法使用位置参数

任务

我想编写一个具有可变数量参数的函数(使用...,该函数调用具有相同参数的另一个函数,并在末尾调用一个新的函数。订单很重要!下面的示例仅用于演示。

我尝试了什么

function foo(...$params) {
    $extraVariable = 6;
    var_dump(...$params, $extraVariable);
}
foo(2, 4, 1, 4);
Run Code Online (Sandbox Code Playgroud)

问题

运行它时,出现以下错误消息:

PHP致命错误:在第3行的/home/user/main.php中解压缩参数后,无法使用位置参数

我如何实现我的目标?

php variadic-functions

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

链接可变参数函数调用

printf类函数从不同的参数个数函数调用很容易足以让-只使用一个v的那些功能(-version vprintfvsprintfCString::FormatV,等)。但是,如果我将呼叫链接起来怎么办?这是简单的代码:

#include <stdarg.h>
#include <iostream>
void direct(const char * _fmt, bool _extra, ...){
    va_list args;
    va_start(args, _extra);

    char ca[200];
    vsprintf(ca, _fmt, args);
    std::cout << ca << std::endl;

    va_end(args);
}

void chained(const char * _fmt, ...){
    va_list args;
    va_start(args, _fmt);
    direct(_fmt, false, args);
    va_end(args);
}

int main(){
    direct("direct works just fine: %d", false, 1);
    chained("indirect produces garbage: %d", 1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

示例输出如下:

direct works just fine: 1
indirect produces garbage: …
Run Code Online (Sandbox Code Playgroud)

c c++ variadic-functions

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

标签 统计

variadic-functions ×10

c ×4

c++ ×3

c++11 ×2

scala ×2

c89 ×1

c99 ×1

constructor ×1

cvi ×1

java ×1

overloading ×1

php ×1

printf ×1

sfinae ×1