Built in C ++ and compiler

!! Specific to commonly used methods like getter and setter. !!

I have no idea when to use the keyword inline

. Ofc I know what he does, but I still don't know.

According to an interview with Bjarne Stroustup, he said:

My own rule of thumb is to use inlining (either explicitly or implicitly) only for simple one- or two-line functions that I know are used a lot and are not likely to change much over the years. Things like the size () function for a vector. The best use of inlining is a function where the body is less code than the function call and return mechanism, so that the inline function is not only faster than the non-string version, but also more compact in the core of the object: smaller and faster.

But I often read that the compiler automatically introduces short functions like getter, setter methods (in this case, it turns out size()

a vector

).

Can anyone please help?

Edit:

Coming back to this after many years and experience of high performance C +++ programming, inline can really help. Sometimes in the games industry even forceinline matters because not all compilers work the same way. Some of them may automatically embed some of them. My advice: if you are working on frameworks, libraries or any heavily used code, consider using inline, but this is just a general guideline since you want such code to be fully optimized for any compiler. It may not be best to always use inline, because you will also need a class definition for that part of the code. This can sometimes increase compile time if you can no longer use forward declarations.

another hint: you can use C ++ 14 automatic type return even when splitting the function definition:

MyClass.h

class MyClass
{
    int myint;
public:
    auto GetInt() const;
}  

inline auto MyClass::GetInt() const { return myint; }

      

all in one .h file.

+3


source to share


6 answers


I answer my own question !: Solution: After a few performance tests, the correct rule from Stroustrup is correct! inlining Short functions like .size () from a vector can improve performance (.size () calls are often used). But the effect is only noticeable for FREQUENTLY used functions. If the getter / setter method is used a lot, nesting it can increase performance.

Stroustrup:



Don’t make statements about "efficiency" of code without first doing time measurements. Guesses about performance are most unreliable.

0


source


Actually, the keyword is inline

not for the compiler anymore, but for the linker.

That is, while the inline

function declaration still serves as a hint to most compilers, with a high optimization setting, they will inline elements without inline

and will not inline elements with inline

if they deem it better for the resulting code.



In cases where it is still necessary to mark the signs of a function as weak and thus bypass one definition rule, which says that in a given set of object files that you want to be written in binary, each character (such as a function) must be present only once.

+5


source


Bjarne's quote is old. Modern compilers are pretty smart.

However, if you are not using Generation Time Code Generation, the compiler should see the code to insert it. For functions used in multiple .cpp files, this means you need to define them in the header. And to get around the one definition rule in this case, you must define these functions as inline

.

Class members defined within a class inline

are the default.

+3


source


The following is specifically for C ++:

The keyword inline

has nothing to do with attachment.

The keyword inline

allows you to use the same function multiple times in the same program:

Each program must contain exactly one definition of each non-built-in function or variable that is odr-used in that program; no diagnostics required.

Β§3.2 [basic.def.odr]

Attaching a meaning, other than that, to a keyword inline

is erroneous. The compiler is free to inline (or not) according to the "as is" rule:

A conforming implementation executing a well-formed program must produce the same observable behavior as one of the possible executions of a corresponding abstract machine instance with the same program and the same input.

Β§1.9 [intro.execution]

+1


source


Considering what compiler optimizations can do, the only one I'm currently using is a non-template function whose body is defined inside header files outside of class classes .

Everything is defined (note: defined! = Declared) inside the body of the body is inline by default, just like templates.

The meaning inline

is actually: "Defined in a header, potentially imported from multiple sources, just keep only one copy of it" as specified by the linker.

Maybe in C ++ 35 someone will finally decide to replace this keyword with another more meaningful one.

+1


source


C ++ Standard Section 7.1.2 Clause 2:

(...) An inline specifier indicates an implementation that inline function body substitution at the point of call should be preferred over a normal function invocation mechanism. The implementation is not required to do this inline replacement at the point call (...)

In other words, instead of havin one code for your function that gets called multiple times, the compiler can simply duplicate your code in different places called by the function. This avoids the small overhead associated with calling a function at the cost of large executables.

Remember that the keyword inline

can also be used with namespaces, but with a completely different meaning. Members of an embedded namespace can be used in most cases as if they were members of the enclosing namespace. (see standard, section 7.3.1, clause 8).

Edit: the google style guide only recommends inline if the function is ten lines or less.

0


source







All Articles