Answer the question
In order to leave comments, you need to log in
How does the C language: 1) implement the concept of "inline" function? 2) Is it possible to implement a common function name across different types?
Hello! I continue to study the C language.
1)
Now I understand macros, and what can be built on their basis. I came across such concepts as built-in, inline functions. If I understand correctly, then the point is that if the function is small enough and does relatively little work, then it would be much more efficient to paste the code of this function in the place where it is used in order to avoid calling the function? Is this task taken over by the compiler itself? If so, how does he implement it?
For example, I could come up with only such a mechanism:
#include <stdio.h>
long unsigned str_len(char const *beg)
{
char const *end;
for (end = beg; *end != '\0'; ++end)
;
return end - beg;
}
#define INLINE_STR_LEN(beg, len) \
{ \
char const *end__; \
\
for (end__ = beg; *end__ != '\0'; ++end__) \
; \
len = end__ - beg; \
}
int main(void)
{
char *str = "Hello, world!";
long unsigned len;
// len = str_len(str);
INLINE_STR_LEN(str, len)
printf("len = %lu\n", len);
return 0;
}
#include "fold.h"
#define FOLD(N, T, Op) \
T N##_##T(T const *a, long unsigned n) \
{ \
T r; \
\
for (r = *a++; --n != 0; r Op *a++) \
; \
return r; \
}
FOLD(sum , int , +=)
FOLD(product, int , *=)
FOLD(sum , double, +=)
FOLD(product, double, *=)
#ifndef FOLD_H
#define FOLD_H
#define FOLD(N, T) T N##_##T(T const *a, long unsigned n);
FOLD(sum , int)
FOLD(product, int)
FOLD(sum , double)
FOLD(product, double)
#undef FOLD
#endif
#include <stdio.h>
#include "fold.h"
#define ISIZE 4
#define DSIZE 5
int main(void)
{
int iarr[ISIZE] = { 1 , 2 , 3 , 4 };
double darr[DSIZE] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
printf("The sum of iarr: %d\n" , sum_int (iarr, ISIZE));
printf("The sum of darr: %.1f\n" , sum_double (darr, DSIZE));
printf("The product of iarr: %d\n", product_int (iarr, ISIZE));
printf("The product of darr: %f\n", product_double(darr, DSIZE));
return 0;
}
Answer the question
In order to leave comments, you need to log in
Haha, you're a funny fellow. In general, inline is an advice to the compiler to inline a function, there are also things specific to the gcc or clan, for example, that require you to do this. But since these people have often abused the compiler sometimes ignores the requirements of the programmer. It is better to use special macros like "hot code" or vice versa "cold" in the specification they are. Regarding the implementation of templates in C, try googling templates in c directly. The technology is as old as the world. In the general case, this method of providing polymorphism is of course used, but it is still better to use some kind of void and avoid generalized C code as much as possible. Most application tasks are covered by things like glib, and macros are often used there with a specific idiom, an example would be glib vector
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question