18

The standard says that given a declaration of

inline void foo();

that foo is an inline function with external linkage (because by default all function declarations have external linkage). This strikes me as odd. because the one definition rule section 3.2 (in both C++03 and C++11) say:

3 ... An inline function shall be defined in every translation unit in which it is used.

5 There can be more than one definition of a[n] ... inline function with external linkage (7.1.2) ... Given such an entity named D defined in more than one translation unit ... each definition of D shall consist of the same sequence of tokens

This means that an inline function might as well have internal linkage, because use of the function in any way through external linkage (that is, across translation units) would be to invoke undefined behavior (by paragraph 3), and that the content of the inline function in all translation units needs to be the same.

Is there a backwards compatability or specific toolchain reason for this rule?

Community
  • 1
  • 1
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • I believe [this](http://www.glenmccl.com/ansi_015.htm) answers the Question. – Alok Save Feb 16 '12 at 04:11
  • @Als: That discusses the pitfalls, but not the reason why that change was made. – Billy ONeal Feb 16 '12 at 04:16
  • If you read it completely, It says: *"The immediate motivation for this change was a need of the new template compilation model that was adopted at the same meeting; but more generally it was felt that changing the default was an idea whose time had come, and the change was approved unanimously in both ANSI and ISO"*. – Alok Save Feb 16 '12 at 04:16
  • 1
    @Als: Why not put that quote into an answer? :) – Billy ONeal Feb 16 '12 at 04:23
  • 2
    Because I don't have anything more to add to the article I linked,and I feel a bit unfair to gain rep through it.I will add that as an answer and mark it as community wiki, just so the Q and answer are preserved. – Alok Save Feb 16 '12 at 04:30
  • 3
    @Als: I would say, you found the quote, you deserve the reputation. After all, most standardese questions end up being nothing more than citing the Standard, the difficulty is finding the needle in the haystack. – Matthieu M. Feb 16 '12 at 07:22
  • Can you please fix the spelling? i try hard but i cannot make any sense of "to use the function though it is external linkage would be undefined behavior". – Johannes Schaub - litb Feb 16 '12 at 18:14
  • @JohannesSchaub-litb: Ah, yes, meant "through" (though obviously the spell checker isn't smart enough to detect *usage* errors :( ) – Billy ONeal Feb 17 '12 at 02:26
  • @billy im sorry the sentence still doesnt make any sense. – Johannes Schaub - litb Feb 17 '12 at 08:10
  • @JohannesSchaub-litb: I'm not sure what to do with it at this point. (I don't see where the confusion lies) – Billy ONeal Feb 17 '12 at 08:13
  • @billy can you show example code that uses an internal linkage inline function "through it's external linkage"? – Johannes Schaub - litb Feb 17 '12 at 09:34
  • @JohannesSchaub-litb: Oh, I see the confusion. Is that better? – Billy ONeal Feb 17 '12 at 17:01

2 Answers2

18

One result of that decision is that a static variable defined within an inline function will be shared between all instantiations of the function. If the default had been internal linkage, each translation unit would have gotten its own copy of the static variable. That's not how people expect things to work - inline vs. non-inline shouldn't affect the code semantics so drastically.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
9

This is aptly answered here by Jonathan Schilling's article: Extern Inlines By Default.

To quote him about motivation for this change:

The immediate motivation for this change was a need of the new template compilation model that was adopted at the same meeting; but more generally it was felt that changing the default was an idea whose time had come, and the change was approved unanimously in both ANSI and ISO.

Eliah Kagan
  • 1,704
  • 3
  • 22
  • 38
Alok Save
  • 202,538
  • 53
  • 430
  • 533