Inline functions in c++ - conditions - c++

Under What condition an inline function ceases to be an inline function and acts as any other function?

The Myth:
inline is just a suggestion which a compiler may or may not abide to. A good compiler will anyways do what needs to be done.
The Truth:
inline usually indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism. An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules(especially w.r.t One Definition Rule) for inline are followed.
Under What condition an inline function ceases to be an inline function and acts as any other function?
Given the quoted fact there is a deeper context to this question.
When you declare a function as static inline function, the function acts like any other static function and the keyword inline has no importance anymore, it becomes redundant.
The static keyword on the function forces the inline function to have an internal linkage.(inline functions have external linkage)
Each instance of such a function is treated as a separate function(address of each function is different) and each instance of these functions have their own copies of static local variables & string literals(an inline function has only one copy of these ).

It's at the discretion of the compiler.
But some cases just can't be inlined, like:
Recursive functions
Functions whose address is referenced somewhere
Virtual functions (there are some exceptions thought)

That depends on the compiler optimization.
Different compilers have different rules to make the code more efficient. But if you declare a function as inline, the compiler tends to respect your decision as long as none of it's rules says different.
Remember that the compiler can change completely the execution path of a method. For example, consider the next situation:
int MyClass::getValue()
{
return someVariable;
}
At compile time, there is little difference between declaring this kind of function as inline or not. Probably the compiler will make the attribute partially public and code:
myInstance->getValue()
as
myInstance->someVariable
So it's more an aesthetical decision in most cases.

Related

inline this function, does make sense?

I have the next member function:
template <typename T>
inline T Foo::Read(const DWORD addr) const // Passing by value.
{
T buffer;
ReadProcessMemory(m_processHandle, (LPVOID)addr, &buffer, sizeof(T), NULL);
return buffer;
}
If I'm not wrong, when the compiler inlines a function, it avoids calling the function and put the code of the called function into the caller function.
So in the caller function (assuming an integer return type) I would want something like:
ReadProcessMemory(m_processHandle, (LPVOID)addr, &bufferOfTheCaller, sizeof(int), NULL);
I have three questions about this:
1) What would happen with the variable that we return from the function?
Isn't the declaration of the variable buffer performed in run time?
2) In this case ReadProcessMemory is a huge function from the WinAPI, should the compiler still able to inline this function?
3) What is the difference between leaving the member function defined inside the class definition and declare it with the keyword inline outside the class definition? If I want to use inline keyword, Do I have to put the inlined function in the same file .h or?
It's important to note that the inline keyword has nothing to do with inlining calls to the function. All it does is allow the function to be defined in multiple translation units, as long as all of the definitions are the same.
What the inline keyword does is let you define a function in a header file that will be included in multiple translation units. This may give the compiler a better opportunity to inline call to that function, since it has the full definition available in multiple translation units, but it is not a command or even a hint that you want the compiler to do so. The compiler will decide on its own if it should or shouldn't inline a call to any given function. There are compiler-specific extensions that you can use if you do want to force calls to a function to be inlined, but that is not what the inline keyword does.
With that out of the way, I'll add that the compiler inlining a function call doesn't change the rules of C++ at all. It isn't a textual replacement, like a preprocessor macro. The compiler will figure out how to insert the logic from the called function into the caller. At that point, things like C++ variables don't really exist. If your call looks something like this:
int someValue = myFoo.Read(someAddress);
then the compiler can easily transform that into something like this:
int someValue;
ReadProcessMemory(myFoo.m_processHandle, (LPVOID)someAddress, &someValue, sizeof(int), NULL);
due to the as-if rule. Both of those snippets result in the same observable behavior, so the compiler can freely transform between them.
What would happen with the variable that we return from the function?
It will be destroyed, because the return value was discarded by the function call expression.
Isn't the declaration of the variable buffer performed in run time?
Declarations happen at compile time.
In this case ReadProcessMemory is a huge function from the WinAPI, should the compiler still able to inline this function?
If the compiler knows the definition of the function, then it could expand it inline. Whether it should, or whether it will do so depend on many factors. Size of a function is a heuristic that may affect the choice that the compiler makes.
What is the difference between leaving the member function defined inside the class definition and declare it with the keyword inline outside the class definition?
In one case the definition is inside the class and in the other case it is outside. There is no other difference.
If I want to use inline keyword, Do I have to put the inlined function in the same file .h or?
If you want to define a member function inline, but want to define it outside of the class definition, then you must declare the function inline within the class definition - except a function template, which is implicitly inline.
If you want to define the member function within the class definition, then you don't need to explicitly declare it inline; it will be so implicitly.
If you want to not define the function inline, then you must define the function outside the class definition and must not use the inline keyword.
If I'm not wrong, when the compiler inlines a function, it avoids calling the function and put the code of the called function into the caller function.
Yes, but inline has little to do with that.
1) What would happen with the variable that we return from the function? Isn't the declaration of the variable buffer performed in run time?
The compiler, inlining or not, will have to reserve some space for buffer, typically on the stack.
In other words, there is no bufferOfTheCaller. If your function is inlined, buffer will be in the caller's stack frame; otherwise, it will be put in the callee's stack frame.
2) In this case ReadProcessMemory is a huge function from the WinAPI, should the compiler still able to inline this function?
It does not matter how big the implementation of ReadProcessMemory is, your code just performs a function call to it, which is tiny. An optimizing compiler is likely to inline your function.
3) What is the difference between leaving the member function defined inside the class definition and declare it with the keyword inline outside the class definition?
No difference.
If I want to use inline keyword, Do I have to put the inlined function in the same file .h or?
The inline keyword is not about inlining. If you want to put the definition of a function in a header file, you will likely need inline to prevent redefinition errors.

Inline const function

As inline function will replace the actual call from code, what is the use of calling inline function as const.
Inline void adddata() const {...}
An inline function is one which can be defined in each translation unit, and must be defined separately in each translation unit where it is called. It is also a completely non binding suggestion to the compiler that you think the function should be inline. The compiler is free to actually inline or not inline any of your functions, whether or not they are declared inline.
const means that the object the function is a method of will tend not to be visibly modified by the function call. There are exceptions to this, it is always possible to modify if you try hard enough, but in general const is a promise to the caller that you won't.
Using them together means nothing additional to their individual meanings. They are essentially unrelated.

Should I use static or inline?

I am writing a header-only library, and I can’t make up my mind between declaring the functions I provide to the user static or inline. Is there any reason why I should prefer one to the other in that case?
They both provide different functionalities.
There are two implications of using the inline keyword(§ 7.1.3/4):
It hints the compiler that substitution of function body at the point of call is preferable over the usual function call mechanism.
Even if the inline substitution is omitted, the other rules(especially w.r.t One Definition Rule) for inline are followed.
The static keyword on the function forces the inline function to have an internal linkage(inline functions have external linkage) Each instance of such a function is treated as a separate function(address of each function is different) and each instance of these functions have their own copies of static local variables & string literals(an inline function has only one copy of these)
Note: this answer is for C++. For C, see caf's answer. The two languages differ.
static has two relevant meanings:
For a function at namespace scope, static gives the function internal linkage, which in practical terms means that the name is not visible to the linker. static can also be used this way for data, but for data this usage was deprecated in C++03 (§D.2 in C++03 Annex D, normative). Still, constants have internal linkage by default (it's not a good idea to make that explicit, since the usage for data is deprecated).
For a function in a class, static removes the implicit this argument, so that the function can be called without an object of the class.
In a header one would usually not use internal linkage for functions, because one doesn't want a function to be duplicated in every compilation unit where the header is included.
A common convention is to instead use a nested namespace called detail, when one needs classes or functions that are not part of the public module interface, and wants to reduce the pollution the ordinary namespace (i.e., reduce the potential for name conflict). This convention is used by the Boost library. In the same way as with include guard symbols this convention signifies a lack of module support in current C++, where one is essentially reduced to simulating some crucial language features via conventions.
The word inline also has two relevant meanings:
For a function at namespace scope it tells the compiler that the definition of the function is intentionally provided in every compilation unit where it’s used. In practical terms this makes the linker ignore multiple definitions of the function, and makes it possible to define non-template functions in header files. There is no corresponding language feature for data, although templates can be used to simulate the inline effect for data.
It also, unfortunately, gives the compiler a strong hint that calls to the function should preferably be expanded “inline” in the machine code.
The first meaning is the only guaranteed meaning of inline.
In general, apply inline to every function definition in a header file. There is one exception, namely a function defined directly in a class definition. Such a function is automatically declared inline (i.e., you avoid linker protests without explicitly adding the word inline, so that one practical usage in this context is to apply inline to a function declaration within a class, to tell a reader that a definition of that function follows later in the header file).
So, while it appears that you are a bit confused about the meanings of static and inline – they're not interchangable! – you’re essentially right that static and inline are somehow connected. Moving a (free) function out of a class to namespace scope, you would change static → inline, and moving a function from namespace scope into a class, you would change inline → static. Although it isn’t a common thing to do, I have found it to be not uncommon while refactoring header-only code.
Summing up:
Use inline for every namespace scope function defined in a header. In particular, do use inline for a specialization of a function template, since function templates can only be fully specialized, and the full specialization is an ordinary function. Failure to apply inline to a function template specialization in a header file, will in general cause linking errors.
Use some special nested namespace e.g. called detail to avoid pollution with internal implementation detail names.
Use static for static class members.
Don't use static to make explicit that a constant has internal linkage, because this use of static is deprecated in C++03 (even though apparently the deprecation was removed in C++11).
Keep in mind that while inline can’t be applied to data, it is possible to achieve just about the same (in-practice) effect by using templates. But where you do need some big chunk of shared constant data, implemented in a header, I recommend producing a reference to the data via an inline function. It’s much easier to code up, and much easier to understand for a reader of the code. :-)
static and inline are orthogonal. In otherwords, you can have either or both or none. Each has its own separate uses which determine whether or not to use them.
If that function is not sharing the class state: static. To make a function static can benefit from being called at anywhere.
If the function is relatively small and clear: inline
(This answer is based on the C99 rules; your question is tagged with both C and C++, and the rules in C++ may well be different)
If you declare the function as just inline, then the definition of your function is an inline definition. The compiler is not required to use the inline definition for all calls to the function in the translation unit - it is allowed to assume that there is an external definition of the same function provided in another translation unit, and use that for some or all calls. In the case of a header-only library, there will not be such an external definition, so programs using it may fail at link time with a missing definition.
On the other hand, you may declare the function as both inline and static. In this case, the compiler must use the definition you have provided for all calls to the function in the translation unit, whether it actually inlines them or not. This is appropriate for a header-only library (although the compiler is likely to behave exactly the same for a function declared inline static as for one declared static only, in both cases inlining where it feels it would be beneficial, so in practice there is probably little to be gained over static only).

Why are class member functions inlined?

I think my question has been asked here before, I did read them but still little confused and therefore asking to make it clear.
The C++ standard says all member functions defined inside class definition are inline
I have also heard that compiler can ignore inlining of a function. Will that be true in the above case or it will be always inlined if defined inside class definition?
Also, what was the reason behind this design, making all functions defined inside class definition inline? And what inlining has to do with source and header files?
Update: So one should always define their functions outside class if not to be inlined, right?
Update 2 by JohnB: Two functions declared inside class definition could never call each other as they would have to each contain the whole body of the other function. What will happen in this case? (Already answered by Emilio Garavaglia)
Confusion arises because inline has two effects:
It tells the compiler that the function code can be expanded where the function is called, instead of effectively being called.
It tells the compiler that the function definition can be repeated.
Point 1. is "archaic" in the sense that the compiler can in fact do what it likes in order to optimize code. It will always "inline" machine code if it can and find convenient to do and it will never do that if it cannot.
Point 2. is the actual meaning of the term: if you define (specify the body) a function in the header, since a header can be included in more sources, you must tell the compiler to inform the linker about the definition duplicates, so that they can be merged.
Now, by the language specification, free functions (not defined in class bodies) are by default not defined as inline, so defining in a header a thing like
void myfunc()
{}
if the header is included in more sources, then linked in a same output, the linker will report a multiple definition error, hence the need to define it as
inline void fn()
{}
For class members, the default is the opposite: if you just declare them, they will not be inlined. If you define them, they will be inline.
So a header should look like
//header file
class myclass
{
public:
void fn1()
{} //defined into the class, so inlined by default
void fn2();
};
inline void myclass::fn2()
{} //defined outside the class, so explicit inline is needed
And if myclass::fn2() definition goes into a proper source, must lose the inline keyword.
The inline keyword has for a function 2 meanings:
Code replacement: Wherever inline function is invoked, don't generate a function call for it but simply place the contents of the function
at the place of its call (this is something similar to macro
replacement, but type safe)
One definition rule: Don't generate multiple definition for a inline function, only generate a single definition common for all (exception: static functions)
The 1st terminology ("Code replacement"), is simply a request to the compiler. which can be ignored as compiler is better to judge whether to put the text or a function call. (for example, virtual functions or recursive functions cannot be inlined).
The 2nd terminology ("One definition rule") is guaranteed to happen by any conforming compiler. This will generate only 1 definition for all translation units. This facility eases coder's work sometimes, as for smaller function one may not want to put its definition in .cpp file (e.g. getters, setters).
Moreover, for template function which are header only constructs, this effect is mandatory. Thus template functions are inline by default.
Examples:
class A {
public:
void setMember (int i) { m_i = i; }
};
In this example mostly compiler would suffice both terminologies
class A {
inline virtual ~A () = 0;
};
A::~A() {}
Here compiler can only suffice the 2nd requirement.
The only reason to make the method function inline is if you define it in the header.
If you define a method function in a header, and you do not put inline keyword, and you include the header in several header or source files, you would get multiple definition of the method.
c++11 standard in 9.3/2 Member functions [class.mfct] tells :
A member function may be defined (8.4) in its class definition, in which case it is an inline member function (7.1.2) ...
When the definition is inside the class, it is treated as if it were declared inline, because it is assumed that class definitions live in header files that are used from more than one translation unit, so any non-inline definitions here would violate the One Definition Rule.
The compiler is, as always, free to inline whatever it thinks as long as it takes care that functions that are either explicitly or implicitly inline will not lead to linker errors. How it does that is left open by the language spec -- inlining the function of course works, but it is also acceptable to demote the symbol visibility or rename the symbol to a translation unit specific name (as if the function were in an anonymous namespace), or (as most of them do) communicate to the linker that multiple copies of that function may exist and that it should discard all but one of them.
So, in short, it is not treated any different from functions that are explicitly declared inline.
The compiler can ignore inlining if specified by the inline keyword. If the method implementation is present inside the class definition, that's a different thing, and can't be ignored. (well it can, but that makes the compiler non-conforming)
The reason behind the desing - I'm assuming a mechanism was needed where you can actually force the compiler to actually inline your functions, since the inline keyword doesn't mandate it. But in general, inline method definition is done only in cases like getter and setter methods, or some trivial 2-liners. And templates, but that's a different issue.
Inlining has to do with headers and source files in that the definition of the function must be visible to the compiler so it knows how to actually inline the call. It's more difficult to inline a function defined in an implementation file than one defined in a header.
EDIT: On a side note, the paragraph the op is reffering to is 7.1.2.3:
A function defined within a class definition is a inline function [...].
EDIT2:
Apparently, there are some difference between an inline function and inline substitution. The first is a property of a function, that doesn't only include inline substitution, the second means that the function body is actually pasted where it is called.
So the function can be inlined but not have its body pasted instead of being called.
the two things you reffer to are different aspects and not to be confused with.
1) The C++ standard says all member functions defined inside class definition are inline
2) I have also heard that compiler can ignore inlining of a function
1) is when you define the member functions inside the class declaration itself. ie: in the header files. for that you do not have to provide any keyword( ie: inline)
2) You can specify a function as inline by explicitly using the inline keyword. this is actually a request to the compiler. the compiler may or may not make the function inline according to some rules of optimization.

inline in definition and declaration

During a recent peer review, another Software Engineer suggested that I state that the inline function is inline in the definition (outside of the class) as well as in the declaration (inside of the class). His argument is that "By marking it inline, you are saying that this method will be executed much faster than a non-inline method, and that callers don't have to worry about excessive calls to the method."
Is that true? If I am a user of a class, do I really care about excessive calls to the method? Is there anything wrong with listing it as inline in both the definition and declaration? The C++ FAQ states:
Best practice: only in the definition outside the class body.
So who is right here?
That sounds like two totally unrelated things. Putting inline at both the declaration in the class and the definition outside of the class is not needed. Putting it at one of the declarations is enough.
If you talk about adding inline in a .cpp file where a function is defined there, and that function is a public member, then you should not do that. People who call inline functions must have their definitions visible to them. The C++ Standard requires that.
By marking it inline, you are saying that this method will be executed much faster than a non-inline method, and that callers don't have to worry about excessive calls to the method.
That's nonsense. Marking a function inline doesn't guarantee that the function will actually be physically inlined; even if it is, that's no guarantee that your function will be "faster".
By marking the definition inline as well as the declaration, you're just confusing things by pretending to your user that there's any guarantee about anything, which there isn't...
If I am a user of a class, do I really care about excessive calls to the method?
Not really.
In fact, really, the only time you should write inline is when you need to force inline storage for some reason (regardless of whether inlining occurs, using the keyword always affects the application of the one-definition rule to your function… though requiring this is rare); otherwise, let the compiler decide which functions to inline, and move on. The corollary of this is that you don't need to worry about using the keyword to pretend that it's documenting anything.
A function definition defined in the header file should use the inline specifier.
example double get_f(){return f;} defined in foo.h
should use the inline specifier as in:
inline double f_get{return f};
According to the C++ guidelineS.
As for inlining outside the header I have not seen any information that would suggest it is needed.