可变参数模板示例 [英] Variadic Templates example

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

问题描述

考虑以下代码,我不明白为什么必须定义空白打印功能。

Consider following code, I don't understand why empty function of print must be defined.

#include <iostream>
using namespace std;

void print()
{   
}   

    template <typename T, typename... Types>
void print (const T& firstArg, const Types&... args)
{   
    cout << firstArg << endl; // print first argument
    print(args...); // call print() for remaining arguments
}

int main()
{   
    int i=1;
    int  j=2;
    char c = 'T';
    print(i,"hello",j,i,c,"word");

}   


推荐答案

正确的方法:

CORRECT WAY:

可变参数模板与归纳严格相关

Variadic templates is strictly related to induction, a mathematical concept.

编译器解析以下函数调用

The compiler resolves the following function call

print('a', 3, 4.0f);

进入

std::cout<< 'a' <<std::endl;
print(3, 4.0f);

已解析为

std::cout<< 'a' <<std::endl;
std::cout<< 3 <<std::endl;
print( 4.0f);

已解析为

std::cout<< 'a' <<std::endl;
std::cout<< 3 <<std::endl;
std::cout<< 4.0f <<std::endl;
print();

此时,它会搜索匹配为空函数的函数重载。

At this point it searches for a function overload whose match is the empty function.


  • 所有具有1个或多个参数的函数都与可变参数模板匹配

  • 所有没有参数的函数都将匹配到空函数

罪魁祸首是,对于每种可能的参数组合,您只能拥有一个函数。

The culprit is that you must have, for every possible combination of parameters, only 1 function.

错误1:

ERROR 1:

执行以下操作将出错

template< typename T>
void print( const T& arg) // first version
{   
    cout<< arg<<endl;
}   

template <typename T, typename... Types>
void print (const T& firstArg, const Types&... args) // second version
{   
    cout << firstArg << endl; // print first argument
    print(args...); // call print() for remaining arguments
}

因为调用 print 编译器不知道要调用哪个函数。

Because when you call print the compiler doesn't know which function to call.

执行 print(3)是指第一还是第二版本?两者都是有效的,因为第一个参数有1个参数,第二个参数也可以接受一个参数。

Does print(3) refers to "first" or "second" version? Both would be valid because the first has 1 parameter, and the second can accept one parameter too.

print(3); // error, ambiguous, which one you want to call, the 1st or the 2nd?






错误2:


ERROR 2:

以下内容仍然是错误

// No empty function

template <typename T, typename... Types>
void print (const T& firstArg, const Types&... args) 
{   
    cout << firstArg << endl; // print first argument
    print(args...); // call print() for remaining arguments
}

实际上,如果您使用它

 print('k', 0, 6.5);

已解析为

 std::cout<<'k'<<std::endl;
 print(0, 6.5);

已解析为

 std::cout<<'k'<<std::endl;
 std::cout<< 0 <<std::endl;
 print( 6.5);

已解析为

 std::cout<<'k'<<std::endl;
 std::cout<< 0 <<std::endl;
 std::cout<< 6.5 <<std::endl;
 print(); //Oops error, no function 'print' to call with no arguments

尝试,编译器尝试不带任何参数调用 print()。但是,如果不存在这样的函数,则不会调用它,这就是为什么您应该提供该空函数(不用担心,编译器会优化代码,因此空函数不会降低性能)。

As you see in the last attempt, the compiler tries to call print() with no arguments. However if such a function does not exists it is not called, and that's why you should provide that empty function (don't worry, the compiler will optimize code so empty functions don't decrease performance).

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

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