这里为这个模板函数是否生成不同的实例? [英] here different instances are generated for this template function or not?

查看:105
本文介绍了这里为这个模板函数是否生成不同的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下结构和模板函数,每次使用具有不同的num值的函数时,构建函数的新实例,或者因为 const numbers & nums 参数是一个引用,将被实现为一个指针所有使用不同的值num可以被定向到函数的一个实例?

Considering following struct and template function, does each use of the function with a different value for "num" build a new instance of the function or since const numbers<num> & nums parameter is a reference and would be implemented as a pointer all uses with different values for "num" can be directed to one instance of the function?

template<size_t num>    
struct numbers{
 public:
  unsigned int nums[num];
};

template<size_t num>
void print(const numbers<num> & nums,size_t size){
 for (int i=0;i<size;i++)
  cout <<nums.nums[i]<<' ';
 cout <<'\n';
}


推荐答案

程序员), print< 2>() print 3()是单独的函数。虽然编译器在理论上可以进行这种优化,但它可能不会发生在pratice。找出肯定的最好方法是查看生成的汇编程序。

As far as you (the programmer) are concerned, print<2>() and print<3>() are separate functions. Whilst the compiler may theoretically be able to make this optimisation, it probably won't happen in pratice. The best way to find out for sure is to look at the generated assembler.

这篇关于这里为这个模板函数是否生成不同的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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