Are inline operators good?

JalalJaberi picture JalalJaberi · Jan 9, 2013 · Viewed 16.8k times · Source

Is there any difference between operators and other methods to make inline in C++? I have searched for it, but it is not a common question, as I see. Has anyone a strong reason to use it or avoid? Note: clearly, I mean inline operators when they are small.

Answer

Jerry Coffin picture Jerry Coffin · Jan 9, 2013

While this could vary between compilers, I'd expect that from the compiler's viewpoint, an operator is just another function with a somewhat unusual name that allows the syntax of the source code to look a little different.

Nonetheless, by the time the code generator part of the compiler runs, I'd expect any difference between an overloaded operator and another function (that did the same things) to have disappeared.

As such, declaring it as inline or defining it within the body of a class definition will have just as much (little, depending on your viewpoint) with an operator overload as any other function. I'd normally expect that effect to be pretty minimal in both cases -- at least when optimization is enabled, most compilers pretty much ignore the inline keyword and make their own decision about what to expand inline (and what not to) on their own.

Note that in one way the compiler can't ignore the inline keyword though -- there are some special modifications to the "one definition rule" that must be observed, regardless of whether a function is actually expanded inline or not.