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.
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.
You can use the inline
keyword to suggest to the compiler that a function be made inline.
The compiler is not obligated to obey this request.
Operators are similar - they may or may not be inlined.
Since the compiler can't be forced to inline there are probably no strong reasons to use or avoid using inlining hints. Because that is all they are: hints.
In Visual C++ you can use the __forceinline
keyword to force inlining, the result is larger code and potential loss of performance. It's common in well-designed systems that eliminating options (by forcing things) often results in poorer performance. Even if you use this keyword, not every function can be successfully inlined.
GCC inlining is discussed here.
As others mentioned if the compiler 'inlines' a method or not is usually not on your behalf, regarding usage of the inline
keyword and the optimization strategy of the compiler.
So I think the more important aspect is readability of header files, and operator
methods are likely to appear as sets of e.g. arithmetical operation implementations, where simple transformations can be used to build them up. So if I have to look into such header file, I'd like to see logically related sets of operator method signatures together to get an overview what's applicable or not.
A very simple example is the implementation of operator==()
and operator!=()
:
class A
{
public:
// Equality tests
//-----------------------------------------------------------------------
// This may involve some mor complex code that'll look ugly here
bool operator==(const A& rhs) const;
// Fine, this is a one liner. Code appears 'inline' (and is likely to be
// chosen for inlining by the compiler, no matter if inline keyword or not)
bool operator!=(const A& rhs) const { return !A::operator==(rhs); }
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With