How should FORCEINLINE macro by used in C++?

I absolutely can’t tell you what best practices are, but I can share my opinion.
Which may be a controversial take to be posting on this forum, but I am feeling brave today so here goes…

How should FORCEINLINE macro by used in C++?

It shouldn’t be used at all.

Also, if the function declaration and implementation are separated into a header and .cpp file, do I need specify the FORCELINE at both the declaration and implementation?

If the definition is only in one translation unit, the compiler could only inline it in that translation unit. This is actually what the regular inline keyword is/was for, it allows you to define it in multiple translation units (normally a link time multiple def error) so it can be inlined there (if the compiler decides so).

You should not worry about actually doing this though (and hopefully the thought of having multiple definition for the same function in different translation units makes you uncomfortable, at the least), link time optimisations will do this for you anyway.

This old post What is FORCEINLINE macro? - #2 by Shadowriver suggests FORCEINLINE does indeed do as it says on Windows, presumably still true, but if you think you can decide better than the compiler, I can assure you that you are almost certainly wrong. If profile guided optimisation is also in use, no human stands any chance of competing.

For the example of the if conditions you gave, do what you feel is best for you (hide it in a function, presumably), and don’t worry about optimisation… the compiler/linker will do it for you if it will be benificial.

That’s my take on it anyway. To anyone who holds different view, please post! OP will be intersted in what you have to say, as will I.

1 Like