I have a serious problem to reconcile the "inline concept" with "linkage" with my humble knowledge:
- I've heard we can use
externto access "a non-static entity" (like an entity that is NOT declared with static key) from other files
= YES! I GOT THIS - They say we can use
inlineto make a function or variable appear in a "header file" and so that it can be accessed across multiple translation units
= YES! I GOT THIS - They also say "an inline entity" can have either "internal or
external linkage"
= I DIDN'T GET IT
( A: Inline functions with internal linkage? - This where I first heard about an inline entity with "internal linkage" )
An inline entity can be accessed from anywhere across multiple translation units, so doesn't it mean that "an inline entity" should always have "external linkage" as granted?
What is the point of "an inline entity" which can be accessed from anywhere across multiple translation units and has "INTERNAL linkage"?
Any answer or any confirmation that I was wrong in any point would be appreciated.
The answer you've linked relates to C's
inline, which works slightly differently. However, in either language,inlinedoes not affect linkage.inlineon a function makes it an inline function. By default, any free function has external linkage, which basically means that all functionsfdeclared in multiple translation units refers to the samef.staticwould make make the functions have internal linkage, which means that every translation unit has its ownf.A function doesn't have to be an inline function to be accessible in multiple TUs.
inlinejust means that it can be defined in multiple TUs, not just declared, and this won't cause any linker errors (all definitions must be identical). In other words, the one-definition rule is relaxed for inline functions. And yes, as stated above, free functions have external linkage by default, even if they areinline.None. If it's accessible in multiple translation units, then it shouldn't have internal linkage. The benefit of internal linkage is that you can define it differently with the same name in different TUs.
static inlinein headers is an anti-pattern from C, which is motivated by the problem that unlike in C++, if a function isn't inlined, the linker will looks for anexterndefinition in some object file instead, which may not exist.