模板专门化和实例化 [英] Template specialization and instantiation

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

问题描述

这些概念对我来说有点不清楚。好了,模板实例化由 N4296 :: 14.7 [temp.spec] 定义得很好:

These concepts is a bit unclear to me. Well, template instantiation's defined pretty well by N4296::14.7 [temp.spec]:


实例化函数,类,类
模板或成员模板的成员的行为被称为模板
实例化。

The act of instantiating a function, a class, a member of a class template or a member template is referred to as template instantiation.

这是如果我们有一个函数/变量/类模板,模板的实例化只是创建一个对象或函数。例如:

That's if we have a function/variable/class template, instantiation of the template is just creating an object or function. For instance:

template<typename T> class U{ };
U<int> a; //instantiation

N4296:14.7.1 [temp.inst] / code>说(强调我的):

But N4296:14.7.1 [temp.inst] says (Emphasize mine):


除非明确地显示类模板专用化
实例化(14.7.2)或显式专用化(14.7.3)时,类
模板专用化被隐式实例化,当
专用化在需要a
完全定义的对象类型或类
类型的完整性影响程序的语义。

Unless a class template specialization has been explicitly instantiated (14.7.2) or explicitly specialized (14.7.3), the class template specialization is implicitly instantiated when the specialization is referenced in a context that requires a completely-defined object type or when the completeness of the class type affects the semantics of the program.


推荐答案


模板
specialization的实例化的定义是什么,而不仅仅是模板的实例化?

What's the definition of the instantiation of the template specialization, not just the instantiation of a template?

这是一回事。

实例化模板专用化是指将特定模板参数替换为模板以获取实例化的类,函数等的过程。

实例化一个模板一般只意味着有一些参数,实例化将完成。只给出模板本身。

That's the same thing.
"Instantiating a template specialization" refers to the process of substituting specific template arguments into a template to obtain an instantiated class, function, etc.
Instantiating a template in general solely means that there are some arguments for which instantiation will be done. Only the template itself is given.

例如,

template <typename... T>
void f() {
    std::cout << ... << T();
}

我可以说


  • 我用参数 int 实例化 f b
  • 我实例化 f

  • 我实例化一个函数,它是 f ,特别是 f< int>

  • 我实例化
    a $ c> f
    ,它产生模式的一个实例化。

  • I instantiate f with the argument int
  • I instantiate f<int>
  • I instantiate a function which is a specialization of f, specifically f<int>
  • I am instantiating a pack expansion inside f, which produces one instantiation of the pattern.

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

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