29

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?

4
  • 9
    The only thing that's relevant about inline is that it creates an exemption from ODR. Commented Feb 13, 2012 at 22:01
  • 9
    ""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... Commented Feb 13, 2012 at 22:38
  • 1
    might be executed faster would have been the right choice of words. Commented Nov 11, 2019 at 15:30
  • 1
    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. Commented Nov 6, 2022 at 5:46

3 Answers 3

23

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
@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.
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
@0A0D i don't understand. Perhaps you shall open a new question that specifically addresses that question instead?
It's the same question. If I use the keyword inline at the header and in the cpp file, isn't that redundant?
|
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.

5 Comments

In this case, I am only checking if a particular value is set and returning true or false.
@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. :)
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.
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 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!
-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.

2 Comments

Welcome to Stackoverflow.com. Please read about how to mark code in your answers so it is formatted more nicely.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.