inline function


i come from VB.Net where every method in a class is inline (in fact there is no way to declare functions outside of the class scope such as Class1::Func1)

so i was wondering
1) what exactly is the difference between an inline function and one that is not?

2) is it ok if all my class's functions (even very big ones) are declared inline?
When you use inline functions you recommend to a comppiler to insert this funcion in binary file directly, so it allows you to minimize overhead to call function, but the compilered binary file is larger, because it has the same instructions(functions) more then once.

is the speed significant? or should i say compared to the extra file size incurred, is the speed significant?
it is somewhat, but then most compilers won't even inline functions/methods larger than 3-4 lines.

edit: over = larger than
Last edited on
ic, thanks for the reply
I assume you (the OP) don't bother about any performance considerations but because you are used to write everything in one file or somehow think it just looks cleaner to have all the stuff together. Am I right?

About performance: In typical programs it's really not measurable at all. In days of "Link-Time Code Generation", the compiler has any means of deciding by himself what will be inlined and what not - even if you have it in a separate cpp file.

Anyway, there is another huge difference you should think about: Compile Time. If you inline almost everything, you will recognize, that you have to include a LOT of other files in every file you write. These files include a lot of other files too and so on. Even for mid-size projects (20-30 Files) you will recognize that your compiler has to rebuild every single file on every change to most files. This can become a show-stopper very quickly.

This is something you don't have in VB the same way. In Basic, the "compiler" can extract the symbols of a class without the need to recompile it every time you just import another file. (Same goes for other languages, where implementation and declaration is in one file like Java).

So if you go for "inline everything", be prepared to change your mind after you cross the "20 files" border. ;-)

Ciao, Imi.


pacerier wrote:
i come from VB.Net where every method in a class is inline


I think there is a difference between the way C++ and Visual Basic look at things in this case and the way they organise their class declarations.

In C++, any function definition in the class declaration is merely a request to the compiler to inline the function if possible.

In visual basic - you put all your function definitions in the class declaration and the compiler decides which to inline and which not.

ic, hm ok what would be the advise then. as for best practice, what things should be inline and what should not?
Very generally, I try to inline functions that are small, quick, and/or don't do very much. 'Get' functions which do nothing but return something, for example, should probably always be inlined.

Even if the function is a bit larger you may want to inline it. There's no black and white "inline this but don't inline that" guideline.

See this FAQ:

http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.3

To quote it:

Cpp FAQ Lite wrote:
You have to play with it to see what is best. Do not settle for simplistic answers like, "Never use inline functions" or "Always use inline functions" or "Use inline functions if and only if the function is less than N lines of code." These one-size-fits-all rules may be easy to write down, but they will produce sub-optimal results.



EDIT: doh -- way too slow XD
Last edited on
ic, thanks for all your replies i will keep that in mind
Topic archived. No new replies allowed.