开发者

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

开发者 https://www.devze.com 2023-03-17 22:48 出处:网络
We don\'t have \'ext开发者_开发问答ern template\'s and a \'template\' function is always \'implicit inline\'. Does it make sense anyway?That doesn\'t make sense to me. A function template is not \"imp

We don't have 'ext开发者_开发问答ern template's and a 'template' function is always 'implicit inline'. Does it make sense anyway?


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 the static 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 putting inline on them, though. static and inline aren't dependent in C++ like they are in C99.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号