I have a simple equation that needs to be called about 100 times throughout my code. I am trying keep function overhead to a minimum, but still use OOP concepts (which is new to me). The equation is just a simple one that calculates the max of two differences. It is like 3 lines. Should I use an inline function? If so, what is usually the accepted way to use it (i.e should I create a seperate .h file, put it in a base class(all the objects using this function are indirectly derived).
-
1Herb Sutter on inlining: http://www.gotw.ca/gotw/033.htm --- http://www.drdobbs.com/inline-redux/184403879 – Thomas Eding Jul 12 '13 at 20:32
-
Btw, I should point out that if you're just learning C++, you shouldn't be worrying about performance too much, with performance being pretty much the only reason to inline something. Also note that the `inline` keyword is only a hint to the compiler, and the compiler is free to not inline something, or even to inline things that you didn't explicitly ask to be inlined. – Nicu Stiurca Jul 12 '13 at 20:48
-
1The `inline` keyword is not a "hint" in C++. It doesn't even have anything to do with optimizations. It's simply a way of bypassing the [one definition rule](http://stackoverflow.com/questions/4192170/what-exactly-is-one-definition-rule-in-c) by instructing the linker to accept multiple definitions of the function. – Cody Gray - on strike Jul 12 '13 at 21:00
-
possible duplicate of [When to use inline function and when not to use it?](http://stackoverflow.com/questions/1932311/when-to-use-inline-function-and-when-not-to-use-it), [Benefits of inline functions in C++?](http://stackoverflow.com/q/145838), [Benefits of declaring a function as "inline"?](http://stackoverflow.com/q/1875947) – Cody Gray - on strike Jul 12 '13 at 21:01
-
At SchighSchagh: Performance is a big issue with this program as the processor will only be 1000mhz. That is why I am asking about inlining. Either I macro the function, or I write it out 122 times in the code. I know that inlining is better, because it provides more safety. However, I'm not sure if the function will 'actually' be inlined if I use that keyword. – user2079828 Jul 12 '13 at 21:06
-
There is no mechanism in C++ that *ensures* a function is inlined. That's the job of the compiler. If you can't trust your compiler to make these decisions, then you're wasting your time writing code for it. – Cody Gray - on strike Jul 12 '13 at 21:13
-
@user2079828 If your real goal is optimization, then **you absolutely must profile your code**. If you've already done that, good on you. Otherwise, go do it! You will probably be surprised by what's really slowing you down. – Nicu Stiurca Jul 12 '13 at 21:48
-
@CodyGray Inlining has everything to do with optimization. For short functions such as the one OP describes, the function call mechanism is possibly an unnecessary overhead, and avoiding that overhead is exactly the point of inlining functions. Yes, ODR doesn't apply to inline functions, but that's only to facilitate the actual inline expansion of functions. – Nicu Stiurca Jul 12 '13 at 21:51
-
@sch I'm aware of what inlining is for. But that's not what the `inline` keyword means according to the C++ standard. – Cody Gray - on strike Jul 12 '13 at 21:57
3 Answers
Unless your compiler has good Link Time Optimization features (and they are enabled), the entire function you want to inline must be declared and defined in a header file. As for dedicating a .h file solely for your inlined function, or where to put it in your class hierarchy, we couldn't tell you even if you gave us the rest of your code and/or design. It's really more a stylistic choice than predefined rules about the one and only correct way to do it.
EDIT: To clarify: if your inline function will only be used in a single source (.cpp) file, you can just declare it right in that same file as static inline
. If you want it accessible to multiple source files, then put it in a header file.

- 8,747
- 8
- 40
- 48
-
Thank you, this answers one part of my question. My only other question is how to guarantee the function will be inlined...which I don't think is possible within the scope of the inline keyword. Correct? – user2079828 Jul 12 '13 at 21:12
-
1There is nearly nothing you can do to GUARANTEE that a function is inlined. The compiler may have a `force_inline` or `always_inline` extension, but that really isn't the right way to do things. If the compiler doesn't really want to inline the function, then it's probably for a good reason! – Mats Petersson Jul 12 '13 at 21:22
-
So basically, I must #define to get this functionality, or copy the code to every place that uses it? – user2079828 Jul 12 '13 at 21:23
-
1No, you just use an inline function and let the compiler decide. Inlining a function does not necessarily make the code faster. Not to mention all the other reasons that using a macro is a horrible idea. – Cody Gray - on strike Jul 12 '13 at 21:38
Yes, you can use inline
function members if they are not virtual
. But in every file that uses this inline function should be present it's definition, so it's a good idea to put implementation of this function in *.h file. If it is virtual you can not use inline (although gcc allows it) according to the standard.
But in any case standard does not guarantee inlining your function - even if you mark it as inline
.

- 14,395
- 10
- 44
- 68
-
1You can still use `inline` with `virtual` functions. It will likely reduce the probability the compiler will inline a function (`inline` is a hint, not a guarantee). But, it does allow the compiler to inline virtual functions if the compiler statically knows the actual type at the call site. – Nathan Ernst Jul 12 '13 at 20:54
-
I don't understand the concept of 'hint' to the compiler. There must be a definition of when the compiler will or will not inline a function given the keyword....otherwise it is useless. – user2079828 Jul 12 '13 at 21:20
-
@user2079828, hm... but there is no. Compiler decides itself and if it sees that inline-generated function will be slower or useless it will not define it. – sasha.sochka Jul 12 '13 at 21:51
I would not put such a function in a class unless it needs to use some of the state of the class, or if there is some good reason to limit its use to that class hierarchy.
An inline function inside a namespace in a .h sounds like it would be a good way to go. By putting it in a namespace you get to use the common name max without worrying about it being in the global namespace and you can then collect any functions fitting that domain there.
Though if it's just a max function maybe just using the std::max function would be a good option?
In general it's better to reuse if possible. It saves time and reduces maintenance costs. Everyone pretty much knows what std::max is but some other max takes some figuring out.

- 4,921
- 1
- 24
- 35
-
The max function is just 1 part of another function. The other function is implemented for 122 objects that are all different. They have the same inherited function, but must implement it differently. The max function is the one part that is the same in every one of them. – user2079828 Jul 12 '13 at 21:16
-
When you say you have 122 objects are you meaning that you implemented 122 different classes? That sounds like a really complex class hierarchy! – PeterSW Jul 12 '13 at 21:43
-
Yes. It was not my choice. Each object pools from the same dataset, but must set data based on an intermediate type, and then there are 3 types of operations that are different for each object. @PeterSW: I do not I could use that. Either way, how much time would that save over just using my own? – user2079828 Jul 12 '13 at 21:49
-
Saves a lot of time figuring out where to put the functionality and saves adding more to the namespace and everyone pretty knows what std::max is but some other max takes some figuring out... – PeterSW Jul 12 '13 at 21:57