C + + Proverbs: Understanding the intervention and exclusion of inline

Source: Internet
Author: User
Tags definition class definition

inline function--What a great idea! They look like functions, they produce effects like functions, they are so much better than macros in every way, and you can call them without incurring the cost of a function call. What more can you ask for?

You can actually get more than you think, because avoiding the cost of a function call is just part of the story. In typical cases, compiler optimizations are designed for a continuous code that has no function calls, so when you inline a function, you may be able to enable the compiler to implement context-sensitive special optimizations for the function body. Most compilers do not implement such optimizations for "outlined" function calls.

However, in programming, as in life, there is no free lunch, and the inline function is no exception. The idea behind a inline function is to replace every call to the function with a function ontology, and not to hold the Ph.D in the table. You can see that this may increase the size of your target code. On machines with limited memory, too much enthusiasm for inline can make programs too large for free space. Even with virtual memory, the code bloat caused by inline can result in additional paging scheduling, reduced instruction cache hit rates, and consequent performance losses.

On the other hand, if a inline function ontology is very short, the code generated for the function ontology may be smaller than the code generated for a function call. If this is the case, inline this function can actually result in smaller target code and higher command cache hit rates! Remember, inline is a request made to the compiler, not a command. This request can be presented in an explicit or implicit manner. The implicit approach is to define a function within a class definition:

class Person {
  public:
   ...
   int age() const { return theAge; } // an implicit inline request: age is
   ... // defined in a class definition
  private:
   int theAge;
};

Such a function is usually a member function, but we know that the friend function can also be defined within the class, and if they are there, they are implicitly declared as inline.

The way to explicitly declare a inline function is to add the inline keyword before its declaration. For example, here are the implementation methods that are often used by the standard Max template (from):

template // an explicit inline
inline const T& std::max(const T& a, const T& b) // request: std::max is
{ return a < b ? b : a; } // preceded by "inline"

The fact that Max is a template leads to an observation conclusion: inline functions and templates are generally defined in header files. This leads some programmers to conclude that function templates must be inline. This conclusion is illegal and potentially harmful, so it's worth investigating. The inline function must generally be in the header file because most build environments are inline during compilation. In order to replace a function call with the function ontology of the called function, the compiler must know what the function looks like. (Some build environments can be inline during a connection, and a few-for example, a control environment based on the. NET Common Language Infrastructure (CLI)-can be inline at run time. However, these circumstances are exceptions, not rules. Inline is a compile-time behavior in most C + + programs. )

The template is generally in the header file, because the compiler needs to know what a template looks like so that it is instantiated when it is used. (Again, not all of this.) Some build environments can be instantiated during a connection. However, compile-time instantiation is more common. Template instantiation is independent of inline. If you write a template and you think all of the functions that are instantiated from this template should be inline, declare the template as inline, which is what the implementation of the Std::max above is doing. But if you write a template for a function that has no reason to inline, you should avoid declaring the template as inline (either explicit or implicit). Inline are cost-and you don't want to encounter them without any foresight. We've talked about how inline causes code to swell, but there are other costs that we'll talk about in a moment.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.