简体   繁体   中英

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'. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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