I have read in my document say about inline function. My document says that: there are two types of inline function: implicity function and explicity function.
Explicity function: you use inline keyword before function, and using outside of the class. for example:
inline int Math::add(int a, int b){ return a + b; }
Implicity function: every method inside class is implicity. for example:
class Math {
int add(int a, int b) { return a + b;} // implicity inline function
};
So, if this true, so, every method that I don’t want to use inline, I must declare outside of the class, right ? And if this true, can I implement a method inside class and don’t want inline function.
Thanks 🙂
The only way to guarantee that it is not inline is to make it unreachable at compilation time, for instance, by putting its body definition into cpp file instead of header.
UPDATE: A commenter says that even putting function body into different compilation unit is not guaranteed to help. He is absolutely right. Usually it helps, but some compilers still may inline the function. So, there is no reliable way to disable inlining that is not compiler-dependent.
All the inlining is just a question of optimization. If appropriate optimization is on, by writing an inline keyword you just tell the compiler that you RECOMMEND to inline the function. You can neither force compiler to inline a function, nor force compiler not to inline it. For certain compilers, e.g. VC++, there are ways to do so (
__declspec(noinline)), but they all are compiler-dependent.And why do you need to disable inlining? The compiler often knows better… If it is for debugging purposes, just disable the optimizations, or at least function inlining. You may even use pragmas to do so in a single file. Anyway, debugging a release version should usually be avoided, though sometimes it is impossible to avoid it, of course.