Fabian
Fabian

Reputation: 4211

Should I define static inline methods in a cpp file?

As a followup to the question Should I define static inline methods in header file?

If I have two helper functions in two separate cpp files have the same signature, the compiler will complain about ODR. Is it therefore good style to make free functions in a cpp file static to avoid name clashes and give the linker less work?

More generally asked: What are advantages and disadvantages of making free functions inline, static or static inline in a cpp file?

Upvotes: 0

Views: 341

Answers (1)

Bathsheba
Bathsheba

Reputation: 234885

It's good practice to use an anonymous namespace for such a function:

namespace {
    // your function here; no need for static or inline
}

In this way it's confined solely to that translation unit, not visible to the linker, and so the one definition rule will hold.

Upvotes: 5

Related Questions