是否可以将va_list传递给可变参数模板? [英] Is it possible to pass va_list to variadic template?

查看:273
本文介绍了是否可以将va_list传递给可变参数模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 va_list 通常是您应该避免的事情,因为它不太安全,但是可以通过类似这样的函数传递参数:

I know that va_list is usually something you should avoid since its not very safe, but is it possible to pass the arguments from a function like:

void foo(...);

到类似

template<typename... Args>
void bar(Args... arguments);

编辑:最初我想要尝试使用它来调用具有可变数量的参数/类型的虚函数,但这并不是使该问题变得无关紧要的方法。最终我最终做了这样的事情:

edit: Originally I wanted to try to use this to call a virtual function with a variable amount of arguments / types, but this was not the way to go making this question kind of irrelevant. Eventually I ended up doing something like this:

struct ArgsPackBase
{
    virtual ~ArgsPackBase() {}
};

template<typename... Args>
struct ArgsPack : public ArgsPackBase
{
public:
    ArgsPack(Args... args_)
        : argsTuple(args_...)
    {}

    void call(std::function<void(Args...)> function)
    {
        callExpansion(function, std::index_sequence_for<Args...>{});
    }

private:
    template<std::size_t... I>
    void callExpansion(std::function<void(Args...)> function, std::index_sequence<I...>)
    {
        function(std::get<I>(argsTuple)...);
    }

    std::tuple<Args...> argsTuple;
};


推荐答案

不,可变参数函数是运行时功能,并且您传递给可变参数模板的参数数量(尽管是变量)必须在编译时知道。

No, variadic function arguments are a runtime feature, and the number of arguments you pass to a variadic template, although variable, must be known at the compile time.

这篇关于是否可以将va_list传递给可变参数模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆