帮助我理解variadic模板 [英] Help me understand variadic template please

查看:126
本文介绍了帮助我理解variadic模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的可变参数模板中的第一个代码:

Here is my first code in variadic templates :

void print_f()
{
}

template<typename T, typename ... ARG>
void print_f(const T& a, ARG... C)
{
    std::cout<<a;
    print_f(C...); // -> at this line I want to know
    std::cout<<std::endl;
}



...); ,实际发生了什么?我的意思是
模板参数包 C 得到解包,如何在可变参数模板中完成函数参数扣除,匹配如何完成?

when I make a recursive call to print_f(C...); , what actually happens? what I mean is template parameter pack C gets unpacked and how is function argument deduction is done in variadic templates , how is matching done?

任何人都可以解释一些基础知识吗?

can anyone please explain some basics?

修改:通常在类似

template <typename T> class x{
public:
T aa; // can declare variable of type T
};

但在可变参数模板中:

template<typename T, typename ... ARG>
void print_f(const T& a /* , ARG... C -> if remove this */ )
{
    std::cout<<a;
    ARG... C // and put it here , we can't do this,  why?
    print_f(C...); 
    std::cout<<std::endl;
}


推荐答案

必须也是一个模板,但我不是100%肯定。

I think your first printf overload must also be a template, but I'm not 100% sure.

无论如何,它的工作或多或少像这样:

Anyway, it works more or less like this:

printf("APPLE", 10, '@', 9.0); 
// this calls printf(const char*, int, char, double)
// with T=const char*, and ARG={int, char, double}
    printf(const char* a, ARG ... C) {
    std::cout<<a; // this displays "APPLE"
    print_f(C...); 
    // this calls printf(int, char, double)
    // with T=int, and ARG={char, double}
        printf(int a, ARG ... C) {
        std::cout<<a; // this displays 10
        print_f(C...); 
        // this calls printf(char, double)
        // with T=char, and ARG={double}
            printf(char a, ARG ... C) {
            std::cout<<a; // this displays '@'
            print_f(C...); 
            // this calls printf(double)
            // with T=double, and ARG={}
                printf(double a, ARG ... C) {
                std::cout<<a; // this displays 9.0
                print_f(C...); 
                // this calls printf()
                    printf() {
                    }
                std::cout<<std::endl;
            std::cout<<std::endl;
        std::cout<<std::endl;
    std::cout<<std::endl;

还应注意,使用代码,所有值都显示在同一行,很多空白线。

It should also be noted that with your code, all the values appear on the same line, followed by lots of blank lines. I think you wanted the endl before the recursion.

根据您的编辑: 在你的假设函数 template< typename T,typename ... ARG>
void print_f(const T& a)
,您将无法调用此函数,除非您自己指定ARG,因为它不能从函数参数类型中推导出来,因为ARG不再在参数中。

As per your edit: In your hypothetical function template<typename T, typename ... ARG> void print_f(const T& a), you would be unable to call this function unless you specified ARG yourself, as it cannot be deduced from the function parameter types, since ARG is no longer in the parameters.

此外,可变参数模板不能实例化,因为它们不是类型,它们是一组类型。如果你真的想实例化一些东西,你可以使用一个元组: std :: tuple< ARGS> C; ,但是所有的对象都是默认构造的,因为你没有传递参数到要构造的函数。

Also, variadic templates cannot be instantiated because they are not a type, they are a grouping of types. If you really wanted to instantiate something, you could use a tuple: std::tuple<ARGS> C;, but all the objects would be default constructed, since you didn't pass parameters to the function to construct from.

这篇关于帮助我理解variadic模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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