C++使用静态内联声明模板函数是否有意义?
我们没有“外部模板”,并且“模板”函数始终是“隐式内联”。无论如何,这有意义吗?
We don't have 'extern template's and a 'template' function is always 'implicit inline'. Does it make sense anyway?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我来说没有意义。函数模板不是“隐式内联”。您必须显式声明它“内联”才能使其内联。
这并不意味着对它的调用是否内联。这完全是编译器的决定。由于模板可以在多个翻译单元中定义(假设每个定义都为模板提供相同的行为),因此从我看来,将
inline
放在这样的模板上不会产生太大效果。据我所知,唯一的效果是告诉优化器您希望对函数模板的调用应该内联。将函数模板设为
静态
是完全明智的。这使得您可以在多个翻译单元中定义模板,每个定义具有不同的行为。由于静态
,模板将具有内部链接,这意味着它们对于其翻译单元而言是本地的并且不会冲突。不过,在函数和函数模板上放置
static
与在它们上放置inline
完全无关。static
和inline
在 C++ 中并不依赖于 它们在 C99 中。That doesn't make sense to me. A function template is not "implicit inline". 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. 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.Making a function template
static
is entirely sensible. This makes it so you can have the template defined in multiple translation units, with each definition having a different behavior. Because of thestatic
the templates will have internal linkage which means they are local to their translation unit and won't clash.Putting
static
on functions and function template is entirely independent of puttinginline
on them, though.static
andinline
aren't dependent in C++ like they are in C99.