0

I want to create an API which, on the call of a function, such as getCPUusage(), redirects to a getCPUusage() function for Windows or Linux. So I'm using a glue file api.h :

getCPUusage() {
    #ifdef WIN32
        getCPUusage_windows();
    #endif
    #ifdef __gnu_linux__
       getCPUusage_linux();
    #endif
} 

So I wonder if using inline would be a better solution, since with what I have, the call will be bigger.

My question is the following : is it better to use inlined function for every call in this situation ?

Xaltar
  • 1,688
  • 14
  • 22

2 Answers2

1

It depends on use-case of your program. If consumer is still c++ - then inline has a sense. But just assume that you would like to reuse it inside C, Pascal, Java ... in this case inline is not a case. Caller must export someway stable name over lib, but not from header file. Lib for linux is rather transparent, while on Windows you need apply __dllexport keyword - that is not applicable to inline

Dewfy
  • 23,277
  • 13
  • 73
  • 121
0

The answer is: yes, it is indeed more efficient, but probably not worthwhile:

If you're putting these functions in a class, it is not required that you write down the "inline" keyword in your situation, because you only have a header file (You don't have any cpp-files - according to your description). Functions that are implemented inside the class definition (in the header file) will be automatically seen as inline functions by the compiler. Note however that this is only a "hint" to the compiler. The compiler may still decide to make your function non-inline if that is found to be (by the compiler) more efficient. For small functions like yours, it will most likely produce actual inline functions.

If you're not putting these functions in a class, I don't think you should bother adding inline either, as (as said above) it's only a "hint" and even without those "hints", modern compilers will figure out what functions to inline anyway. Humans are much more likely to be wrong about these things then the compiler anyway.

Aleph
  • 1,209
  • 10
  • 19
  • 2
    unfortunately it is not true, If you place this function to .h file - then you must declare as `inline` - otherwise you will get LINK-time error about twice (or more) definition of the same symbol – Dewfy Apr 25 '13 at 16:08
  • @Dewfy Only if he uses the same name (which will cause problems anyway), which I assume he won't do (who says this function isn't part of a class or namespace). He clearly says that he's using a file "api.h". That means he doesn't have a source file where he separately implements the functions. – Aleph Apr 25 '13 at 16:12
  • What you've written here is true for class member functions, but as @Dewfy points out it's false for free functions. – Mark Ransom Apr 25 '13 at 16:25
  • @Mark Ransom Ah, I see. That's true (I wrote "inside the class definition" but didn't handle the other case). Fixed this. – Aleph Apr 25 '13 at 16:30
  • 1
    Your conclusion is still wrong. If you really want to make sure the compiler will inline something its definition has to be available at the calling point, which can only be done by putting it in the header file - and if it's in the header you need `inline` to prevent multiple definitions. Whole program optimization might help but I'd hate to count on it, see http://stackoverflow.com/questions/5987020/can-the-linker-inline-functions – Mark Ransom Apr 25 '13 at 16:44