简体   繁体   English

C++ 使用 static 内联声明模板 function 是否有意义?

[英]C++ Does it make sense to declare a template function with static inline?

We don't have 'extern template's and a 'template' function is always 'implicit inline'.我们没有“外部模板”和“模板”,function 始终是“隐式内联”。 Does it make sense anyway?无论如何,这有意义吗?

That doesn't make sense to me.这对我来说没有意义。 A function template is not "implicit inline". function 模板不是“隐式内联”。 You have to explicitly declare it "inline" to make it inline.您必须明确声明它“内联”以使其内联。

That doesn't mean that calls to it are or aren't inlined.这并不意味着对它的调用是内联的或不是内联的。 That's entirely the compiler's decision.这完全是编译器的决定。 As a template can be defined in multiple translation units (provided each definition gives the template the same behavior), putting inline on such a template doesn't have much effects, from what I can see.由于模板可以在多个翻译单元中定义(前提是每个定义都为模板提供相同的行为),因此在这样的模板上inline并没有太大的影响,据我所知。 The only effect is to tell the optimizer that you wish that calls to the function template should be inlined, from what I can see.唯一的效果是告诉优化器您希望对 function 模板的调用应该被内联,据我所知。

Making a function template static is entirely sensible.制作 function 模板static是完全明智的。 This makes it so you can have the template defined in multiple translation units, with each definition having a different behavior.这使得您可以在多个翻译单元中定义模板,每个定义都有不同的行为。 Because of the static the templates will have internal linkage which means they are local to their translation unit and won't clash.由于static模板将具有内部链接,这意味着它们位于其翻译单元的本地并且不会发生冲突。

Putting static on functions and function template is entirely independent of putting inline on them, though.不过,将static放在函数上,而 function 模板完全独立于将inline放在它们上面。 static and inline aren't dependent in C++ like they are in C99 . staticinline不像在 C99中那样依赖于 C++ 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM