开发者

Isn't inline premature optimization? [duplicate]

开发者 https://www.devze.com 2023-02-15 14:10 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Inline functions in C++
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Inline functions in C++

Modern compilers are better than programmers at deciding what should be inlined and what should not. Just like, register, shouldn't inlining functions be a job for the compiler only, and be considered p开发者_运维百科remature optimization ?


inline has a double meaning that some are unaware of - it allows a function to be defined in more than one translation unit (i.e. if you define an unbound function in a header and include it from within various translation units, you are forced to declare it inline or the linker would complain about doubly defined symbols).

The second meaning is a hint to the compiler that this function may profit from inlining its machine code at the caller site. You're right, modern compilers/optimizers should be able to figure this out on his own.

My advice is to use inline only when it is needed (first case) and never to pass an optimization hint to the compiler. This way, this crazy double meaning is resolved in your source code.


inline is only tangentially related to optimization.

You should choose to apply inline to a function if you need the exceptions to the one definition rule that it gives you, and leave it out if you don't. Most of the time you can rely on the compiler to perform the appropriate optimizations independent of whether a function is declared inline or not.


Check out this answer: Inlining this function or not?

Essentially, yes, inlining is a task of the compiler at this point. Inlining was initially created to indicate to the compiler that it should try. The keyword is indicate - The compiler has the choice as to whether or not it inlines the function or not.

0

精彩评论

暂无评论...
验证码 换一张
取 消