Isn't premature optimization?

Possible duplicate:
Built-in functions in C ++

Modern compilers are better than programmers in deciding what should be built in and what should not. In the same way as register , should you not include functions for the job only for the compiler and be considered premature optimization?

+4
source share
3 answers

inline has a double meaning that some people donโ€™t know about - it allows you to define a function in more than one translation unit (i.e. if you define an unrelated function in the header and include it from different translation units, you are forced to declare it inline or the linker will complain double-digit characters).

The second value is a hint for the compiler that this function can benefit from embedding its machine code on the callerโ€™s website. You are right, modern compilers / optimizers should be able to figure this out on their own.

My advice is to use inline only when necessary (the first case) and never pass the optimization hint to the compiler. Thus, this crazy double meaning is allowed in your source code.

+7
source

inline only related to optimization.

You must choose to apply inline to the function if you need exceptions to the one definition rule that it gives you, and leave it if you do not. In most cases, you can rely on the compiler to perform appropriate optimizations, whether the inline function is declared or not.

+3
source

Mark this answer: Insert this function or not?

Essentially, yes, inlining is the compiler's task right now. Inlining was originally created to tell the compiler that it should try. The keyword indicates whether the compiler has a choice, whether it builds a function or not.

0
source

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


All Articles