Q:
First of all, inline is annoying. Some books say that the inline keyword should be added before the definition, and the declaration can be omitted. Some say that the inline function will become an inline function when it is added to the Declaration,
Some descriptions and definitions must be consistent. Declare a function in a class. The implementation of the function is external, whether it is to add inline in the internal declaration or to add inline in the external implementation,
Or both of them are added, compilation is successful, and debugging methods cannot be used to see the effect on the program. I can't figure out how to write this inline, but I'm sure.
Yes. The definition part of the inline function should be placed in the header file. Opening the Declaration and definition will cause compilation errors.
In addition, inline can be used with the extern and static keywords. I searched the internet, and Linus, the father of Linux, said "static
Inline "means" we have to have this function, if you use it, but don't
Inline it, then make a static version of it in this compilation unit ".
"Extern inline" means "I actually _ have _ an extern for this function,
But if you want to inline it, here's the inline-version ".
Which of the following statements can explain the usage of static inline and extern inline.
A:
Extern inline indicates that the function has been declared. Because the function itself can be declared multiple times, the effect of extern on the function is only explicit of the hidden attribute of the function.
Extern is useful for non-function objects, because the object declaration results in memory allocation, and extern indicates that the object has been declared, and no memory allocation is required.
Static is the previous C usage. the purpose is to make the function identified by the keyword visible only in the local file, other files of the same program are not visible to the function. in other words, even if your other files contain function definitions with the same name as the parameter table, it will not cause repeated function definitions. because static is only visible to the current file.
Most of the information about the inline function is correct. Let me summarize it for you.
The inline function is just a suggestion for the compiler, so whether it can be inline in the end depends on the meaning of the compiler. If it thinks your function is not complex, it can be expanded at the call point, it will actually be inline, not to say that it will be inline if it is declared. It is just a suggestion to declare inline.
Secondly, because inline functions need to be expanded at the call point, the compiler must be able to see the definition of inline functions everywhere. Otherwise, it will become a non-inline function call. therefore, this requires that each of your files that call the inline function
The inline function is defined. Therefore, it is appropriate to put the inline function in the header file, saving you the trouble of implementing each file. therefore, your declaration and definition must be consistent. In fact, if you
If this inline function is implemented once in each file, you 'd better ensure that each definition is the same. Otherwise, it will cause undefined behavior, that is, if not the definitions in each file are the same
Which one is expanded by the compiler depends on the specific compiler. Therefore, it is best to put the inline function definition in the header file.
The member functions in the class are inline by default. If you give a function in the class when defining the class, of course it is best. if you do not define a member function in the class and you want to inline the function, you must
Add inline, otherwise it will be considered not inline. As mentioned above, the inline function is best placed in the header file, so it is best to implement the inline function of the class in the header file defined by the class.
What you said is to separate the declaration from the implementation. In fact, there will be no compilation errors. I have not tried writing so many programs. if the Declaration and definition are separated, this will cause the compiler to not be able to see the function definition everywhere. Therefore, it can only be found in the file where you implement the definition, think of this function as an inline function (if it can be inline). In other files, it is still considered as a common function.
Here, I think you should understand. Then add inline to the Declaration, and do you want to add inline to the implementation? Let's leave it to LZ.
Extern inline indicates that the function has been declared. Because the function itself can be declared multiple times, the effect of extern on the function is only explicit of the hidden attribute of the function.
Extern is useful for non-function objects, because the object declaration results in memory allocation, and extern indicates that the object has been declared, and no memory allocation is required.
Static is the previous C usage. the purpose is to make the function identified by the keyword visible only in the local file, other files of the same program are not visible to the function. in other words, even if your other files contain function definitions with the same name as the parameter table, it will not cause repeated function definitions. because static is only visible to the current file.
Most of the information about the inline function is correct. Let me summarize it for you.
The inline function is just a suggestion for the compiler, so whether it can be inline in the end depends on the meaning of the compiler. If it thinks your function is not complex, it can be expanded at the call point, it will actually be inline, not to say that it will be inline if it is declared. It is just a suggestion to declare inline.
Secondly, because inline functions need to be expanded at the call point, the compiler must be able to see the definition of inline functions everywhere. Otherwise, it will become a non-inline function call. therefore, this requires that each of your files that call the inline function
The inline function is defined. Therefore, it is appropriate to put the inline function in the header file, saving you the trouble of implementing each file. therefore, your declaration and definition must be consistent. In fact, if you
If this inline function is implemented once in each file, you 'd better ensure that each definition is the same. Otherwise, it will cause undefined behavior, that is, if not the definitions in each file are the same
Which one is expanded by the compiler depends on the specific compiler. Therefore, it is best to put the inline function definition in the header file.
The member functions in the class are inline by default. If you give a function in the class when defining the class, of course it is best. if you do not define a member function in the class and you want to inline the function, you must
Add inline, otherwise it will be considered not inline. As mentioned above, the inline function is best placed in the header file, so it is best to implement the inline function of the class in the header file defined by the class.
What you said is to separate the declaration from the implementation. In fact, there will be no compilation errors. I have not tried writing so many programs. if the Declaration and definition are separated, this will cause the compiler to not be able to see the function definition everywhere. Therefore, it can only be found in the file where you implement the definition, think of this function as an inline function (if it can be inline). In other files, it is still considered as a common function.
Here, I think you should understand. Then add inline to the Declaration, and do you want to add inline to the implementation? Let's leave it to LZ.
You can still see the English version. what you don't understand is that there is not only one definition in the header file, but the definition exists in all the program text files that contain the header file. inline functions can be repeatedly defined. In C ++, constant objects and inline functions can be defined multiple times.
You will understand the function chapter.
Suppose that your function meets the inline conditions.
When the Declaration is to add inline and the definition is not added, the compiler must be able to see the inline declaration during compilation and see the definition at the expansion point. In this way, it is considered as an inline function.
If you declare that there is no inline, but the definition is inline. at this time, if other files that want to call the function see its declaration, it is considered that the function is not inline. Therefore, when the function is called,
Go to the implementation of the function, but accidentally see the inline Declaration, this will cause a link error. if you want to correct it, let the file that calls the function also see the definition of inline, instead
It is only seen during the call. you can add the definition of inline to each file. (If inline is not added, repeated definitions may occur because inline functions can be repeatedly defined ). or
Is another way to modify it. You can remove the inline during definition so that it becomes a common function and the link will not go wrong. if it is the previous method, it is still inline, because it conforms to the inline and
The defined conditions are visible.
If you place the declaration with the definition in the same header file, but do not inline at the declaration, inline at implementation, so that the compiler also inline the function (meeting the two conditions, you can see the inline Statement (although it is defined ).
To sum up, as long as the compiler sees an inline and the definition is everywhere, it can inline the function (assuming that your function is simple enough to inline ), you don't have to worry about the definition or declaration of inline.
Therefore, for convenience, it is defined when inline functions are directly declared and placed in the header file. in this way, other files contain this header file, and the inline function definition appears in each file. inline.
The same is true for member functions of the class. however, the member functions of the class are inline by default, provided that you provide the member function definition during the class definition. if the function definition is not provided during class definition, inline must be added outside the class; otherwise, it will be treated as a normal function.
You can see the C ++ primer chapter on functions.
You can still see the English version. what you don't understand is that there is not only one definition in the header file, but the definition exists in all the program text files that contain the header file. inline functions can be repeatedly defined. In C ++, constant objects and inline functions can be defined multiple times.
You will understand the function chapter.
Suppose that your function meets the inline conditions.
When the Declaration is to add inline and the definition is not added, the compiler must be able to see the inline declaration during compilation and see the definition at the expansion point. In this way, it is considered as an inline function.
If you declare that there is no inline, but the definition is inline. at this time, if other files that want to call the function see its declaration, it is considered that the function is not inline. Therefore, when the function is called,
Go to the implementation of the function, but accidentally see the inline Declaration, this will cause a link error. if you want to correct it, let the file that calls the function also see the definition of inline, instead
It is only seen during the call. you can add the definition of inline to each file. (If inline is not added, repeated definitions may occur because inline functions can be repeatedly defined ). or
Is another way to modify it. You can remove the inline during definition so that it becomes a common function and the link will not go wrong. if it is the previous method, it is still inline, because it conforms to the inline and
The defined conditions are visible.
If you place the declaration with the definition in the same header file, but do not inline at the declaration, inline at implementation, so that the compiler also inline the function (meeting the two conditions, you can see the inline Statement (although it is defined ).
To sum up, as long as the compiler sees an inline and the definition is everywhere, it can inline the function (assuming that your function is simple enough to inline ), you don't have to worry about the definition or declaration of inline.
Therefore, for convenience, it is defined when inline functions are directly declared and placed in the header file. in this way, other files contain this header file, and the inline function definition appears in each file. inline.
The same is true for member functions of the class. however, the member functions of the class are inline by default, provided that you provide the member function definition during the class definition. if the function definition is not provided during class definition, inline must be added outside the class; otherwise, it will be treated as a normal function.
You can see the C ++ primer chapter on functions.