Questions about inlining constructors/destructors and code structure.

Started by
21 comments, last by SiCrane 15 years, 3 months ago
There are multiple kinds of linkage that symbols can have. Inline functions tend to get linked in "link once" linkage, which causes the linker to discard multiple definitions of a symbol. This can also be referred to as "common" or "weak" linkage. Though some linkers define up to all three terms and use them differently. For example, one might declare "link once" to discard duplicates and discard the symbol completely if it is unreferenced, but "common" to behave the same way but not discard the symbol completely if it is unreferenced. Standard linkage that you're thinking about is generally referred to as "external" or "external visible" linkage.
Advertisement
Am I mistaken if I say that this is the real purpose of the inline keyword, whereas as far as inlining goes it is free to do what it wants? (However, without link-time inlining it would be impossible to inline a function whose definition is given in a different compilation unit.)
I don't think that would be an accurate statement. The "real purpose" of the inline keyword is to designate that the function is suitable for inlining. It just that it turns out to be not so useful for that purpose, but it wouldn't be the only such thing in the language (e.g. the register keyword and exception specifications). After all, giving a function weak linkage only allows it to be defined in multiple translation units, and why would you do that unless you intended that function to be inlined? It's a different story for template functions, since you can't know what instantiations are going to be generated in different translation units, but that's why template functions are all implicitly inlined. (Not to mention static class variables being treated as having weak linkage as well.)

This topic is closed to new replies.

Advertisement