inline in definition and declaration
Solution 1
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.
Solution 2
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.
Related videos on Youtube
Admin
Updated on July 09, 2022Comments
-
Admin almost 2 years
During a recent peer review, another Software Engineer suggested that I state that the
inline
function isinline
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?
-
Kerrek SB over 12 yearsThe only thing that's relevant about
inline
is that it creates an exemption from ODR. -
ildjarn over 12 years""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...
-
talekeDskobeDa over 4 yearsmight be executed faster would have been the right choice of words.
-
-
Admin over 12 yearsIn this case, I am only checking if a particular value is set and returning true or false.
-
Lightness Races in Orbit over 12 years@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. :)
-
Admin over 12 yearsSo 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.
-
Johannes Schaub - litb over 12 years@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 itinline
explicitly (and if you put it outside the class definition into the header, you actually have to do so). Although it is redundant to putinline
when you define the function within the class definition. -
Admin over 12 yearsJust 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
-
Steve Jessop over 12 yearsThis 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.
-
Johannes Schaub - litb over 12 years@0A0D i don't understand. Perhaps you shall open a new question that specifically addresses that question instead?
-
Admin over 12 yearsIt's the same question. If I use the keyword
inline
at the header and in the cpp file, isn't that redundant? -
Johannes Schaub - litb over 12 years@0A0D and I answered you that that is redundant.
-
Steve Jessop about 9 yearsI 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. -
Lightness Races in Orbit about 9 years@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!