Skip to content

Are inline operators good?

An answer to this question on Stack Overflow.

Question

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

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.