As I understand it, the built-in function in C ++ is an analogue of C macro. But in the book I read that the compiler deals with the embedding of the built-in function code, while macros are embedded in the C preprocessor. Also, macros can only be single-line, while the built-in functions are not limited in principle (although it is recommended not to make built-in functions more than 2 lines of code). I suspect this is not all the important differences.
- oneDo not forget that in C ++ there are the same macros as in C. It is reasonable to assume that the built-in functions were added for a reason. Macros, by the way, can be done multi-line (visually). - ixSci
- C also has inline functions. In general, the inline keyword is just a hint to the compiler. And the difference is that the macro is deployed in the code at the preprocessing stage. In the case of inline functions, the compiler, if possible, inserts the machine code of the function instead of calling it (using a processor command call, or similar, depending on the architecture). - kisssko
- functions can be overloaded, for example - yrHeTaTeJlb
2 answers
Also, macros can only be single line.
This is a half-truth, because no one bothers to put a backslash ("\") and write from a new line, as if it is a continuation of the previous one.
while the built-in functions in principle are not limited in size
The longer the function, the less likely it is its inline compiler. But, of course, the compiler is not focused on strings, but on operators (taking into account optimization).
In the general case, it is worthwhile to oppose not just inline functions (after all, for modern compilers, the inline keyword itself is not an order or even a recommendation to embed something there), but constexpr . They, like (like global constants, instead of # define-constants) have a number of advantages:
- They are more type safe.
- They take into account scopes
- More readable and predictable result due to the fact that the arguments are calculated rather than substituted
Of course, due to this strict control, inline-functions will not replace macros in some aspects (conditional compilation, some part of code generation), but in the rest - it is worth giving them preference.
An important difference between a macro and inline (it should rather be translated as embedded, rather than built-in) to the function that the macro performs at the preprocessing stage. Previously, a separate process was involved in preprocessing; now the compiler usually makes it himself. But! at the preprocessing stage there are only tokens (roughly speaking, individual words), but there are neither types, nor variables, nor functions, nor code blocks (that is, the visibility rules for functions and variables for macros do not work - the macro is available from the declaration point and to the end of the file or cancel macro). Checking the correctness of what happened will be done only when compiling. Therefore, various "miracles" are possible. For example, a classic ( this function is specially written without brackets, but they will not help much here )
#define max(a,b) a>b?a:b And so we call
max(x+1, b++) and then it will not be exactly what is expected.
If you use the inline function, the behavior will be very predictable.
In general, compilers are now quite smart and can well inline themselves when appropriate. Therefore, in most cases, this keyword is not even necessary.
Also, macros can only be single line.
macros can be so large. the backslash at the end of the line will help with this (the main thing is that after it there are no spaces or other characters besides line breaks).
It is also important that the variables declared inside the inline live to the end of the function (or code block), and the variables declared inside the macro can live in the function calling them. Because of this, name conflicts are possible.
In general, macros and inline functions, although they can do similar things, are still quite different tools. Macros can do such that inline functions can never (a classic example is the X macro ).
- #define max (a, b) a> b? a: b such macros are recommended to always be declared as # define max (a, b) (a)> (b)? (a) :( b) enclosing each parameter in brackets. - kisssko
- of course. I specifically cited for example. Although, even if you add brackets, it will not be better (there are still a lot of bugs left) - KoVadim
- There will be no bugs, if initially it is understood that all this is processed strictly at the level of the text of the code. - kisssko
- If you understand what will be there, then of course there will be "no bugs." But for example, many do not expect that a variable can be incremented twice. - KoVadim 4:08 pm