28

During a recent peer review, another Software Engineer suggested that I state that the inline function is inline in the definition (outside of the class) as well as in the declaration (inside of the class). His argument is that "By marking it inline, you are saying that this method will be executed much faster than a non-inline method, and that callers don't have to worry about excessive calls to the method."

Is that true? If I am a user of a class, do I really care about excessive calls to the method? Is there anything wrong with listing it as inline in both the definition and declaration? The C++ FAQ states:

Best practice: only in the definition outside the class body.

So who is right here?

  • 8
    The only thing that's *relevant* about `inline` is that it creates an exemption from ODR. – Kerrek SB Feb 13 '12 at 22:01
  • 8
    "*"By marking it inline, you are saying that this method will be executed much faster than a non-inline method, and that callers don't have to worry about excessive calls to the method."*" This guy is __far__ from correct. Scary... – ildjarn Feb 13 '12 at 22:38
  • 1
    _might be executed faster_ would have been the right choice of words. – talekeDskobeDa Nov 11 '19 at 15:30
  • For all answers about `inline` no longer has to do anything with inlining: By marking it inline, you have to include it in every translation unit, which uses it. Thus the compiler can do optimization without having to resort to link-time optimization. So the relationship is not totally lost. – Sebastian Nov 06 '22 at 05:46

3 Answers3

22

That sounds like two totally unrelated things. Putting inline at both the declaration in the class and the definition outside of the class is not needed. Putting it at one of the declarations is enough.

If you talk about adding inline in a .cpp file where a function is defined there, and that function is a public member, then you should not do that. People who call inline functions must have their definitions visible to them. The C++ Standard requires that.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 5
    So your recommendation is to put the inline in the header and not in the Cpp file if it is public? In my case, it is public. –  Feb 13 '12 at 21:59
  • 2
    @0A0D no that is not my recommendation. As I hinted at, if you call a `inline` function, the definition of the function needs to be visible to the compiler (it needs to be defined in the translation unit of the call). Put it into the header, in- or outside of the class definition and you can then mark it `inline` explicitly (and if you put it outside the class definition into the header, you actually have to do so). Although it is redundant to put `inline` when you define the function within the class definition. – Johannes Schaub - litb Feb 13 '12 at 22:06
  • Just thought of something.. isn't inline in the header and in the cpp file redundant for case where the code exists in the cpp file for the class and not in the header? This is the crux of my question that I still struggle –  Feb 13 '12 at 22:18
  • @0A0D i don't understand. Perhaps you shall open a new question that specifically addresses that question instead? – Johannes Schaub - litb Feb 13 '12 at 22:30
  • It's the same question. If I use the keyword `inline` at the header and in the cpp file, isn't that redundant? –  Feb 14 '12 at 13:16
  • @0A0D and I answered you that that is redundant. – Johannes Schaub - litb Feb 14 '12 at 21:50
13

By marking it inline, you are saying that this method will be executed much faster than a non-inline method, and that callers don't have to worry about excessive calls to the method.

That's nonsense. Marking a function inline doesn't guarantee that the function will actually be physically inlined; even if it is, that's no guarantee that your function will be "faster".

By marking the definition inline as well as the declaration, you're just confusing things by pretending to your user that there's any guarantee about anything, which there isn't...

If I am a user of a class, do I really care about excessive calls to the method?

Not really.

In fact, really, the only time you should write inline is when you need to force inline storage for some reason (regardless of whether inlining occurs, using the keyword always affects the application of the one-definition rule to your function… though requiring this is rare); otherwise, let the compiler decide which functions to inline, and move on. The corollary of this is that you don't need to worry about using the keyword to pretend that it's documenting anything.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • In this case, I am only checking if a particular value is set and returning true or false. –  Feb 13 '12 at 21:56
  • 3
    @0A0D: Your compiler knows that. And if your users find themselves caring whether the function is inlined for performance reasons, then they are programming wrong. :) – Lightness Races in Orbit Feb 13 '12 at 21:58
  • 2
    This answer seems potentially confusing to me. "Marking a function inline doesn't guarantee that the function will actually be physically inlined", but "the only time you should write inline is when you need to force inline storage". I think it should explain the difference between "inline storage" and "physically inlined", without that the appearance is of a flat contradiction. – Steve Jessop Feb 13 '12 at 22:29
  • I think it's better. Pointing out the ODR as the reason for `inline` is right. Is "inline storage" the proper terminology, though, and if not is there a better term? Normally I just say "inline functions", and I talk about storage only when referring to objects. But in the context of this question of course just saying "inline function" wouldn't add anything or explain the distinction, so I see the need for something more to describe the state of being an inline function, distinct from the state of being inlined at a particular call site. – Steve Jessop Apr 20 '15 at 09:41
  • @Steve I don't entirely disagree but I also cba to expand on it any further; if one needs to know more about what `inline` does then there are plenty of other resources for that! – Lightness Races in Orbit Apr 20 '15 at 09:52
-1

A function definition defined in the header file should use the inline specifier.

example double get_f(){return f;} defined in foo.h should use the inline specifier as in: inline double f_get{return f}; According to the C++ guidelineS. As for inlining outside the header I have not seen any information that would suggest it is needed.

  • Welcome to Stackoverflow.com. Please read about how to mark code in your answers so it is formatted more nicely. – Sebastian Nov 06 '22 at 05:50
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 09 '22 at 01:54