C++ 内联模板函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17667098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
inline template function?
提问by user1899020
Do I need inline
template functions if they are included in several cpp
files? Thanks.
inline
如果模板函数包含在多个cpp
文件中,我是否需要模板函数?谢谢。
template<bool> inline QString GetText();
template<> inline QString GetText<true>() {return "true";}
template<> inline QString GetText<false>() {return "false";}
回答by user541686
You do, because those are full function specializations, and therefore subject to the one-definition rule just like normal functions.
你这样做,因为那些是完整的函数特化,因此就像普通函数一样受单一定义规则的约束。
回答by jogojapan
Yes, you need the inline
specifier there.
是的,你需要inline
那里的说明符。
The ODR (one-definition rule) states that there must be exactly one definition of a variable, function, class, enum or template. Exceptions relevant for your question are listed in §3.2/5 (C++11) (emphasis mine):
ODR(一个定义规则)规定变量、函数、类、枚举或模板必须只有一个定义。与您的问题相关的例外情况列在 §3.2/5 (C++11)(重点是我的)中:
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, and provided the definitions satisfy the following requirements. [...]
类类型(第 9 条)、枚举类型(7.2)、具有外部链接的内联函数(7.1.2)、类模板(第 14 条)、非静态函数模板(14.5.6)可以有多个定义、类模板的静态数据成员 (14.5.1.3)、类模板的成员函数 (14.5.1.1) 或在程序中未指定某些模板参数的模板特化(14.7, 14.5.5),前提是每个定义出现在不同的翻译单元中,并且定义满足以下要求。[...]
Template specializations for which allparameters are specified (i.e. explicit specializations) are not listed there, and §14.7.3/12 says:
指定了所有参数的模板特化(即显式特化)未在此处列出,第 14.7.3/12 节说:
An explicit specialization of a function template is inline only if it is declared with the inline specifier or defined as deleted, and independently of whether its function template is inline. [ Example:
template<class T> void f(T) { /? ... ?/ } template<class T> inline T g(T) { /? ... ?/ } template<> inline void f<>(int) { /? ... ?/ } // OK: inline template<> int g<>(int) { /? ... ?/ } // OK: not inline
— end example ]
函数模板的显式特化仅在使用内联说明符声明或定义为已删除时才是内联的,并且与其函数模板是否内联无关。[ 例子:
template<class T> void f(T) { /? ... ?/ } template<class T> inline T g(T) { /? ... ?/ } template<> inline void f<>(int) { /? ... ?/ } // OK: inline template<> int g<>(int) { /? ... ?/ } // OK: not inline
— 结束示例 ]
回答by ZijingWu
There is no reason for inline for template declaration but not for template full specialization, you don't need to add the inline keyword for the first line but the second and third one need it. But each translation unit, which use the template, need to contains the template definition so the best way is to include it in header file and include in other cpps which use it.
模板声明没有理由内联,但模板完全专业化没有理由,您不需要为第一行添加内联关键字,但第二行和第三行需要它。但是每个使用模板的翻译单元都需要包含模板定义,因此最好的方法是将其包含在头文件中,并包含在其他使用它的 cpp 中。
In C++ standard n3376 for 3.2/6, there can be more than one definition of class template for the whole application, given the definition is same.
在 3.2/6 的 C++ 标准 n3376 中,在定义相同的情况下,整个应用程序可以有多个类模板的定义。
===============
================
Update the answere base on Jesse Good comments, (need inline for template full sepcialization) Thanks Jesse Good point out that.
更新基于 Jesse Good 评论的答案,(需要内联模板完全分离)感谢 Jesse Good 指出这一点。
回答by Chen
It seem that the template method must be defined in the same file which is building, You don't need to use the 'inline' keyword for they were build in each cpp file which include it.
似乎模板方法必须在正在构建的同一个文件中定义,您不需要使用 'inline' 关键字,因为它们是在包含它的每个 cpp 文件中构建的。