inline in definition and declaration

前端 未结 2 1649
谎友^
谎友^ 2020-12-15 04:56

During a recent peer review, another Software Engineer suggested that I state that the inline function is inline in the definition (outside of the

相关标签:
2条回答
  • 2020-12-15 05:32

    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.

    0 讨论(0)
  • 2020-12-15 05:40

    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.

    0 讨论(0)
提交回复
热议问题