Is this a valid use of inline functions?

Say I have this code (don't mind that SecondsToMinutes and MinutesToHours are copies of each other)

inline float SecondsToMinutes(float seconds) { return seconds / 60.0; } inline float MinutesToHours(float minutes) { return minutes / 60.0; } inline float HoursToDays(float minutes) { return minutes / 24.0; } inline float SeconndsToHours(float seconds) { return MinutesToHours(SecondsToMinutes(seconds)); } inline float MinutesToDays(float minutes) { return HoursToDays(MinutesToHours(minutes)); } inline float SeconndsDays(float seconds) { return MinutesToDays(SecondsToMinutes(seconds)); } 

Is this a valid use of inline? Does this make sense? Is this a good practice? In the end, if I remember correctly, inline means that function calls are replaced by functional bodies, therefore

 return MinutesToDays(SecondsToMinutes(seconds)) 

should be equivalent

 return seconds / 60.0 / 60.0 / 24.0 

Right?

Or is it better to use macros for this?

 #define EXCHANGE_SEC_MIN(x) (x / 60.0) #define EXCHANGE_MIN_H(x) (x / 60.0) #define EXCHANGE_H_D(x) (x / 24.0) #define EXCHANGE_SEC_H(x) (EXCHANGE_MIN_H(EXCHANGE_SEC_MIN(x))) #define EXCHANGE_MIN_D(x) (EXCHANGE_H_D(EXCHANGE_MIN_H(x))) #define EXCHANGE_SEC_D(x) (EXCHANGE_MIN_D(EXCHANGE_SEC_MIN(x))) 

Which one is better? Or not? I would like other cents on this.

+6
source share
4 answers

Is this a valid use of inline? Does it make sense?

OK, yes, but no.

At the moment, this does not interfere, but does not do what, in your opinion, it does.

In a great post on inline deft_code correctly says:

It is said that inline hints to the compiler that you think the function should be inline . Perhaps it was in 1998, but a decade later the compiler does not need such hints. Not to mention people are usually mistaken when it comes to code optimization, so most flat out compilers ignore the "hint".

Thus, it will not hurt anyone if you do this, but the chance that your compiler will listen to your hint will be practically equal to 0. If he considers it necessary to embed the code, he will do it himself.

inline is currently used primarily for the linker, as it allows multiple definitions in multiple compilation units.

If you want your code to be as fast as possible, and you have access to C ++ 11, you should use constexpr :

 constexpr float SecondsToMinutes(float seconds) { return seconds / 60.0; } //etc.. 
+6
source

Is this a valid use of inline? Does this make sense? Is this a good practice? In the end, if I remember correctly, inline means that function calls are replaced by functional bodies, therefore

Sure. You make it more picky, which is always good.

 return seconds / 60.0 / 60.0 / 24.0 

Yes, how does it work. Or should. inline is just a hint, compilation just might decide otherwise. But for such a single liner, the compiler will embed it.

Macros? What for? If this can be done using functions, why use macros?

+7
source

inline does not mean that function calls are replaced by function bodies. At least, this did not mean that for the last fifteen years: optimizers can no longer accept orders from the developer and will carry out the attachment, whether you indicated inline .

inline actually means that this function can be defined several times, and the linker needs to figure it out and save at most one definition at the end. I am responsible for ensuring that all definitions are identical. "

If you really want to really implement inline (the actual introduction of the function body inside the caller) yourself, you will have to use compiler-specific extensions like __attribute__((always_inline)) .

Usually you need inline when functions are defined in the header, since this header will eventually be included in several translation units, so the definitions will be duplicated. Thus, if your code is inside the header, it is useful to use inline .

+3
source

Assuming that the definitions of your built-in functions are visible to the compiler at the point of use (for example, they are in the header file, which #include d is in each compilation unit as necessary), then your use is valid.

However, inline is just a hint to the compiler. Standards allow the compiler to ignore this hint rather than embed a function. The criteria by which compilers do not perform built-in functions is highly dependent on the compiler.

Macros are an alternative, but there are other problems ... including the fact that they do not respect the program area, and it is easy to write macros - intentionally or accidentally - this leads to the behavior that a person can expect. Therefore, built-in functions are often considered preferable.

0
source

Source: https://habr.com/ru/post/1011752/


All Articles