标签: variadic-functions

C:使用stdarg.h的变量参数列表

我正在尝试变量参数列表并看到一些奇怪的结果......

我正在测试的代码是:

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

void foo(int param1, int param2, ...)
{
    int param3 = 0;

    va_list ap;
    va_start(ap, param2);
    param3 = va_arg(ap, int);
    va_end(ap);

    printf("param3: %d\n", param3);
}


int main(void)
{
  foo(1,1);
  foo(1,1,42);

}
Run Code Online (Sandbox Code Playgroud)

该代码段的输出是:

param3: -1073748472
param3: 42
Run Code Online (Sandbox Code Playgroud)

对于第二个调用:'foo(1,1,42)',一切似乎都按预期工作.

对于第一个调用:'foo(1,1)',结果看起来像一个未初始化的int,虽然我在函数开始时首次初始化时将其设置为0.

我希望能够依赖于如果未调用参数,结果变量应该具有值0的事实.我原本以为va_arg()会足够明智地处理它,但似乎并非如此.

有什么建议可以处理吗?

非常感谢.

c variadic-functions

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

解释varargs的Scala语法

可能重复:
语法糖:_*

我写了一个函数,它传递了一个格式字符串(对于String.format(...))和一个varargs参数数组(以及其他东西).该方法如下所示:

def myMethod(foo: Number, formatStr: String, params: Any*): Unit = {
  // .. some stuff with foo
  println(formatStr, params.map(_.asInstanceOf[AnyRef]) : _*)
}
Run Code Online (Sandbox Code Playgroud)

我在这里得到了params参数的语法.有用!但是怎么样?我不理解第二个参数的语法println,尤其是结尾部分(: _*).显然,调用map并将数组扩展为AnyRefs 序列.

syntax scala variadic-functions

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

Variadic模板,获取函数参数值

我的问题如下:我有一个声明为这样的类:

template<typename ReturnType, typename... Args>
class API
{
    ReturnType operator()(Args... args)
    {
       // Get argument 0
       // Get argument 1
    }
};
Run Code Online (Sandbox Code Playgroud)

我需要通过一个来获取参数,到目前为止我唯一的方法(但我无法让它工作)正在使用std::get,如下:

std::get<0>(args);
Run Code Online (Sandbox Code Playgroud)

当然,这会导致很多错误.我是variadic模板的新手(根本不是C++ 11),所以我在这一点上很失落.

我怎么能逐一得到这些论点?任何帮助将不胜感激.

c++ variadic-functions variadic-templates c++11

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

变量函数(va_arg)不适用于float,而printf有效吗?有什么区别?

我碰巧在两年的这个问题中遇到了类似的情况:

变量函数(va_arg)不适用于float?

有人说,当我们称之为的时候,问题是促使浮动加倍

    va_arg(arg, float)
Run Code Online (Sandbox Code Playgroud)

我的问题是在这篇文章的最后,但首先让我们来看看@ Jack在上面链接的问题下面的答案:

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

void foo(int n, ...)
{   
    va_list vl;
    va_start(vl, n);

    int c; 
    double val; 

    for(c = 0; c < n; c++) {
        val = va_arg(vl, double);
        printf("%f\n", val);
    }

    va_end(vl);
}


int main(void)
{
  foo(2, 3.3f, 4.4f);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

3.300000
4.400000
Run Code Online (Sandbox Code Playgroud)

现在,如果我们改变val = va_arg(vl, double)val = va_arg(vl, float),我们会得到(至少我得到了2012 MSVS):

36893488147419103000.000000
2.162500
Run Code Online (Sandbox Code Playgroud)

我们现在回答我的问题.

在这个主题:C/C++ va_list没有正确地返回参数 最多的投票答案,它的评论说,这printf促进了 float 's to …

c c++ floating-point variadic-functions

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

无法覆盖使用模板参数包扩展声明的虚方法

我无法覆盖使用模板参数包扩展指定的基类的虚方法 - 而重写方法将显示实际的相关类型.这是一个MCVE:

#include <iostream>
template <typename... Ts>
class A { virtual void foo(Ts&&...); };

class B : public A<int, unsigned> {
    void foo(int x, unsigned y) override { std::cout << "here"; }
};

int main() {
    B b;
}
Run Code Online (Sandbox Code Playgroud)

编译它(标准设置为C++ 11或C++ 14),我得到:

a.cpp:9:7: error: ‘void B::foo(int, unsigned int)’ marked override, but does not override
  void foo(int x, unsigned y) override {
       ^
Run Code Online (Sandbox Code Playgroud)

c++ virtual-functions variadic-functions variadic-templates c++11

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

具有部分应用函数的变量参数

我有以下代码的编译问题.

object Main {
    def main(args:Array[String]) = {
      def collectBigger(median:Int)(values:Int*) = values.filter { _ > median }
      val passedRanks = collectBigger(5)_
      //this compiles
      println(passedRanks(Seq(5,9,5,2,1,3)))
      //this doesn't
      println(passedRanks(5,9,5,2,1,3))
    }
}
Run Code Online (Sandbox Code Playgroud)

该示例的灵感来自com.agical.gsl,这是一个swala的scala适配器.我假设它在scala 2.8之前使用了scala功能.

错误是too many arguments for method apply: (v1: Seq[Int])Seq[Int] in trait Function1和变量参数如何传递给部分应用函数有关.

感谢您提供的任何提示.

scala variadic-functions partialfunction

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

Free不会释放作为va_arg列表发送的指针

我想用下面的freeS宏释放一个变量指针列表:freeS(s1,s2,...);

尽管从freeSF函数获取了第一个指针地址的打印,但我的代码并没有释放第一个指针.在主要的,免费(s1)工作,但它应该.正如预期的那样在主要崩溃中自由(s2).

如何释放freeSF函数中的s1指针?

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

#define freeS(...) freeSF("", __VA_ARGS__, NULL)
void freeSF(char *paramType, ...) {
    va_list pl;

    va_start(pl, paramType);
    paramType = va_arg(pl, char *);
    while (paramType) {
        printf("arg %p\n", paramType);
        free(paramType);
        paramType = va_arg(pl, char *);
    }
    va_end(pl);
}

int main(int ARGC, char** ARGV) {
    char *s1 = strdup("1");
    char *s2 = strdup("2");
    printf("s1 %p, s2 %p\n", s1, s2);
    freeS(s1, s2);
    free(s1);
}
Run Code Online (Sandbox Code Playgroud)

c linux variadic-functions

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

替代printf参数有限吗?

Misra 2004有以下规则:

Rule 16.1: Functions shall not be defined with variable numbers of arguments
Run Code Online (Sandbox Code Playgroud)

因此,类似的功能printf不能与规则16.1一起使用.

uint32_t debug_print(char *format, ...)
{
   int int_ret_val=0;

   uint32_t ret_val = ERR_NO_ERROR;
   va_list arguments;
   va_start(arguments, format);

   ret_val = vprintf(format, arguments);

   va_end(arguments);

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

我寻找替代方案,但没有找到任何.

用于记录字符串格式化消息(" %d,%f,..")的所有c系列命令是否都使用变量列表?

c printf misra variadic-functions

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

Do Elixir functions have a maximum airty?

Is there an upper limit to the number of arguments a function can accept? This doesn't count pattern matching. E.g. a function defined like fn ([arg1, arg2, arg3], arg4) only counts as 2.

If there is not an upper limit, does Elixir also support variadic functions?

function elixir variadic-functions

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

如何使用va_start()?

在带有可变参数的函数中,我们使用va_start()函数将类型为va_list,'ap'的对象初始化为:

void va_start(va_list ap, parmN);
Run Code Online (Sandbox Code Playgroud)

我不明白1.

什么类型的对象可以作为parMN(最后一个已知参数)传递。我已经完成了传递整数,带有格式说明符的字符串,结构等

的示例。2. parMN如何描述以下可选参数。

c variadic-functions

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