How does inline function work
Recover your password. Introduction to Command Line Arguments in C? Please enter your comment! Please enter your name here. You have entered an incorrect email address! What is Artificial Intelligence? What is Machine Learning? After the execution of the program, the called function returns the control to the calling program with a return value. This concept of function saves program space because instead of writing same code multiple times the function stored in a place can be simply used by calling it at a desired place in the program.
This might be handy to reduce the program size but it definitely increases the execution time of the program as the function is invoked every time the control is passed to the function and returns a value after execution. Because define macros are evil in 4 different ways: evil 1 , evil 2 , evil 3 , and evil 4. Unlike define macros, inline functions avoid infamous macro errors since inline functions always evaluate every argument exactly once. In other words, invoking an inline function is semantically just like invoking a regular function, only faster:.
Also unlike macros, argument types are checked, and necessary conversions are performed correctly. The declaration of an inline member function looks just like the declaration of a non- inline member function:. This is often more convenient than the alternative of defining your inline functions outside the class body.
However, although it is easier on the person who writes the class, it is harder on all the readers since it mixes what a class does the external behavior with how it does it the implementation. This approach is further exploited in the next FAQ. Here is an example of an inline member function defined outside the class body :.
Recall that you should define your inline member function outside the class body when your class is intended to be highly reused and your reusers will read your header file to determine what the class does — its observable semantics or external behavior. In that case…. Sometimes it is necessary for the compiler to emit a stand-alone copy of the object code for a function even though it is an inline function - for instance if it is necessary to take the address of the function, or if it can't be inlined in some particular context, or perhaps if optimization has been turned off.
And of course, if you use a compiler that doesn't understand inline , you'll need a stand-alone copy of the object code so that all the calls actually work at all. There are various ways to define inline functions; any given kind of definition might definitely emit stand-alone object code, definitely not emit stand-alone object code, or only emit stand-alone object code if it is known to be needed.
Sometimes this can lead to duplication of object code, which is a potential problem for following reasons:. If any of these are a problem for you then you will want to use a strategy that avoids duplication.
These are discussed below. The specification for inline is section 6. Unfortunately this isn't freely available. The following possibilities exist:. A function where all the declarations including the definition mention inline and never extern. There must be a definition in the same translation unit. The standard refers to this as an inline definition. No stand-alone object code is emitted, so this definition can't be called from another translation unit.
You can 1 have a separate not inline definition in another translation unit, and the compiler might choose either that or the inline definition. Such functions may not contain modifiable static variables, and may not refer to static variables or functions elsewhere in the source file where they are declared. In this example, all the declarations and definitions use inline but not extern :.
The function won't be callable from other files; if another file has a definition that might be used instead.
0コメント