为什么需要内联模板专业化? [英] Why do templates specialisations need to be inlined?

查看:77
本文介绍了为什么需要内联模板专业化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是这个答案:

https:// stackoverflow .com / a / 4447057/930315

我遇到了与上述问题的OP类似的问题,
具有功能

I ran into a similar issue as the OP of the cited question, having a function

template<typename T>
void func(T& val);

及其专业化

template<>
void func<mytype>(mytype& val);

导致重复的符号链接器错误(方法在 .tpp文件中实现包括在标题的末尾)。
向专门功能添加内联解决了该问题。为什么?

resulted in a duplicate symbols linker error (the methods are implemented in a '.tpp' file that is included at the end of my header). adding inline to the specialised function resolved the issue. Why?

推荐答案

根据c ++标准的条款3.2:4

According to clause 3.2:4 in the c++ standard

每个程序都应准确地包含该程序中使用的每个非内联
函数或变量的一个定义;无需诊断
。该定义可以显式出现在程序中,可以在标准库或用户定义的库中找到
,或者(适当时,在
时)可以隐式定义(请参见12.1、12.4和
12.8)。内联函数应在使用过的每个翻译单元中定义。

Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see 12.1, 12.4 and 12.8). An inline function shall be defined in every translation unit in which it is odr-used.

这说明了为什么存在链接时间未将专用函数声明为内联时发生错误。该程序将包含专用功能的多个定义,每个模块的一个定义都包括.tpp文件,这将打破标准的条件。声明专用函数 inline 时,它将使该函数满足同一子句的第二部分,即,必须在每个模块中使用该函数定义一个内联函数。

This explains why there is a link-time error when the specialized function is not declared inline. The program will contain multiple definitions of the specialized function, one from each module including the .tpp-file and this breaks the condition from the standard. When declaring the specialized function inline it will make the function satisfy the second part of the same clause, i.e. that an inline function must be defined in each module using the function.

当参数化函数不专用时,将在第3.2:6节中讨论:

When the parameterized function is not specialized it is covered by clause 3.2:6:


一个类类型(第9条),
枚举类型(7.2),具有外部链接的内联函数(7.1.2),
类模板(第14条),不可以有多个定义-static函数模板(14.5.6),类模板的
静态数据成员(14.5.1.3),
a类模板的成员函数(14.5.1.1)或某些$ b的模板专门化程序
中未指定$ b模板参数(14.7、14.5.5),条件是每个定义出现在不同的翻译单位中

There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data member of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.5) in a program provided that each definition appears in a different translation unit

此子句指出对于与l相同的模板函数的多个定义可以ong,因为代码中未指定至少一个模板参数。这允许仅在本地信息上决定是否应该在模块中实例化参数化功能。

This clause states that it is OK for multiple definitions of the same template function as long as at least one of the template parameters is not specified in the code. This is to allow the decision on whether the parameterized function should be instantiated in a module to be made on local information only.

这篇关于为什么需要内联模板专业化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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