0xbadf00d
0xbadf00d

Reputation: 18216

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?

Upvotes: 4

Views: 2479

Answers (1)

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507423

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.

Upvotes: 6

Related Questions